Friedrich Ewald My Personal Website

Posts


  • Java Enum with values

    I wanted to be able to parse Java enums from a JSON file by not only referring to the numerical value of the enum and also the text in the JSON file should not be all uppercase. I wanted to be able to choose a different display value from the inner value in the Java code. So I came up with this code:

    /**
     * This is an enum which has an inner value for each
     * enumeration value. This allows parsing not only from
     * numerical values but also by their inner name.
     */
    public enum MySampleEnum {
        STARTING("starting"),
        RUNNING("running");
        STOPPING("stopping");
    
        /**
         * Inner value of the enumeration.
         */
        private String text;
    
        /**
         * Private constructor.
         * @param text Inner value of the enumeration.
         */
        private JobRunTypeEnum(String text) {
            this.text = text;
        }
    
        /**
         * Returns the string representation of the current enumeration.
         * @return Value of the enumeration.
         */
        @Override
        public String toString() {
            return text;
        }
    	/**
    	 * Gets the right enumeration value from a given String, if it exists.
    	 * @param text The String to search for.
    	 * @return Enum value if found, null otherwise.
    	 */
    	public static MySampleEnum fromString(String text) {
    	    if (text != null) {
    	        for (MySampleEnum val : MySampleEnum.values()) {
    	            if (text.equalsIgnoreCase(val.text)) {
    	                return val;
    	            }
    	        }
    	    }
    	    return null;
    	}
    }
    
    To achieve this, it is necessary to overwrite the default constructor of the enum and give it a parameter which is a String with the inner value. Also there needs to be a private field to hold the value. The toString() method should return the inner value and is therefore overridden. The best thing about this approach is in my eyes, that I am now able to parse the enum from any String just by calling MySampleEnum.fromString("running"). This allows me to integrate this in my JSON-parser and create more meaningful JSON objects.

  • Python Base62 Encoder & Decoder

    Base62 encoding is commonly used for ids where space matters. Many URL shortener service use this and also youtube calculates its video ids in Base62. The advantages are obvious. Instead of using the id 1337, you can just use LZ. This saves 50% (in this case, usually more) of the overall traffic needed to transmit this id. However, Base62 is not an ecryption. I created an easy to use Python module and put it on my GitHub account. As I try to improve code quality, this module has 100% code coverage and also passes all unit tests.

  • Less wide Tabs in VIM

    To use less space in vim I decided to go with only two spaces for each tab stop. This also improves readability in my eyes. I set the following in my .vimrc which I found here:

    set tabstop=4       " The number of spaces count for a TAB.
    set softtabstop=2   " The number of spaces inserted when typing TAB. If not expandtab, type TAB twice, will get one TAB.
    set shiftwidth=2    " The number of spaces when auto-indent.
    set expandtab       " Use the spaces only.
    set smarttab        " Insert appropriate spaces in front of line according to shiftwidth, tabstop, softtabstop.
    set autoindent
    set smartindent
    
    Interesting: Now I need two tabs to have the correct indent for Markdown source code.

  • Using a Newtonsoft.JSON DLL in multpiple projects

    When building a Visual Studio solution, I had the problem that I got a TargetInvocationException when calling a method from a referenced DLL file. When running the unit tests on the DLL file, everything seemed to work fine and I could not track down the problem. The inner exception of the exception brought me then on the right track: The Newtonsoft.JSON module was installed in different versions across each DLL file and the main project.

    ' Write the exception to the console.
    Try
            response = Await client.Login("a", "b")
        Catch ex As Exception
            Console.WriteLine(ex.InnerException)
        End Try
    
    The solution is simple. I just had to make sure, that the DLL is used in the same version in every project. This can simply be done with the console of the package manager.
    PM> Update-Package Newtonsoft.JSON -reinstall
    PM> Update-Package Newtonsoft.JSON
    

  • Experiences with Jekyll (2016)

    My first post on this blog was in December 2015 and since then, this blog is running with Jekyll. I want to take the time to summarize my experiences with Jekyll over the past year. The first thing I did, was creating a build script which I then used within a git-hook. The build script just runs jekyll build and then uses rsync to synchronize the changes to my webserver. This way, I can keep the webserver very light, serving only HTML, CSS and some JavaScript. This script is located in the root folder of the site.

    #!/bin/bash
    jekyll build
    rsync --update --progress -r _site/* [email protected]:/.../html/
    
    Now, every time I write a new post, commit it, it will be automatically published on the web and the corresponding html sites will be updated. This also works for any change in any template file.Since I wanted to allow some dynamic content on my website, I added Disqus. This adds a comment box under each blog post via JavaScript. With this technique, I am able to keep the sites static but also allow feedback. The Javascript is added just before the closing body tag which keeps the loading times very low. Google Page Speed Insights rates this page currently with a score of 71/100 for mobile and 85/100 for desktop. This is obviously far away from the optimal solution and will be a topic in another post. What I can see so far, the changes to the code should be minor and can be easily integrated into the build script. The theme for this site is from the page Jekyllthemes. This page hosts a lot of open source themes for Jekyll which are ready to use. A theme consists of a bunch of files, which have to be places in the the main folder. An easy to understand tutorial can be found here. Overall my experience with Jekyll is very positive and it makes creating a website very easy. Of course one loses a lot of dynamic features, but the point that I don’t have to worry about any security issue ever, justifies this decision. Also, load times are much fast, especially on a shared server.

Page: 28 of 32