Thursday, September 25, 2014

Don't write boilerplate code for java objects any longer

When you write java classes, you usually have many properties you expose via getter/setter methods.
This gives a lot of code, which is not very interesting to write and maintain, but for the sake of java bean (and other reasons) you will do it correctly.

It's one of the features of the IDE that you can let them generate the proper getter/setter methods.
Usually it's called something like "Encapsulate property access". You then select the properties you wish setter/getter created and you are done, the idea puts the correct code in your class.

Nice, but, it could be simpler.
The drawback of all this is, that you fill up your class file with a lot of set/get code which you usually don't want to see.

Fortunally there is help for this as well.
Look at the project Lombook.

With this project you write your class like this:

01 import lombok.AccessLevel;
02 import lombok.Setter;
03 import lombok.Data;
04 import lombok.ToString;
05
06 @Data public class DataExample {
07   private final String name;
08   @Setter(AccessLevel.PACKAGEprivate int age;
09   private double score;
10   private String[] tags;
11   
12   @ToString(includeFieldNames=true)
13   @Data(staticConstructor="of")
14   public static class Exercise<T> {
15     private final String name;
16     private final T value;
17   }
18 }
 
These 18 lines of code do the same as these 118 lines of plain java coding:
001 import java.util.Arrays;

002
003 public class DataExample {
004   private final String name;
005   private int age;
006   private double score;
007   private String[] tags;
008   
009   public DataExample(String name) {
010     this.name = name;
011   }
012   
013   public String getName() {
014     return this.name;
015   }
016   
017   void setAge(int age) {
018     this.age = age;
019   }
020   
021   public int getAge() {
022     return this.age;
023   }
024   
025   public void setScore(double score) {
026     this.score = score;
027   }
028   
029   public double getScore() {
030     return this.score;
031   }
032   
033   public String[] getTags() {
034     return this.tags;
035   }
036   
037   public void setTags(String[] tags) {
038     this.tags = tags;
039   }
040   
041   @Override public String toString() {
042     return "DataExample(" this.getName() ", " this.getAge() ", " this.getScore() ", " + Arrays.deepToString(this.getTags()) ")";
043   }
044   
045   protected boolean canEqual(Object other) {
046     return other instanceof DataExample;
047   }
048   
049   @Override public boolean equals(Object o) {
050     if (o == thisreturn true;
051     if (!(instanceof DataExample)) return false;
052     DataExample other = (DataExampleo;
053     if (!other.canEqual((Object)this)) return false;
054     if (this.getName() == null ? other.getName() != null : !this.getName().equals(other.getName())) return false;
055     if (this.getAge() != other.getAge()) return false;
056     if (Double.compare(this.getScore(), other.getScore()) != 0return false;
057     if (!Arrays.deepEquals(this.getTags(), other.getTags())) return false;
058     return true;
059   }
060   
061   @Override public int hashCode() {
062     final int PRIME = 59;
063     int result = 1;
064     final long temp1 = Double.doubleToLongBits(this.getScore());
065     result = (result*PRIME(this.getName() == null this.getName().hashCode());
066     result = (result*PRIMEthis.getAge();
067     result = (result*PRIME(int)(temp1 ^ (temp1 >>> 32));
068     result = (result*PRIME+ Arrays.deepHashCode(this.getTags());
069     return result;
070   }
071   
072   public static class Exercise<T> {
073     private final String name;
074     private final T value;
075     
076     private Exercise(String name, T value) {
077       this.name = name;
078       this.value = value;
079     }
080     
081     public static <T> Exercise<T> of(String name, T value) {
082       return new Exercise<T>(name, value);
083     }
084     
085     public String getName() {
086       return this.name;
087     }
088     
089     public T getValue() {
090       return this.value;
091     }
092     
093     @Override public String toString() {
094       return "Exercise(name=" this.getName() ", value=" this.getValue() ")";
095     }
096     
097     protected boolean canEqual(Object other) {
098       return other instanceof Exercise;
099     }
100     
101     @Override public boolean equals(Object o) {
102       if (o == thisreturn true;
103       if (!(instanceof Exercise)) return false;
104       Exercise<?> other = (Exercise<?>o;
105       if (!other.canEqual((Object)this)) return false;
106       if (this.getName() == null ? other.getValue() != null : !this.getName().equals(other.getName())) return false;
107       if (this.getValue() == null ? other.getValue() != null : !this.getValue().equals(other.getValue())) return false;
108       return true;
109     }
110     
111     @Override public int hashCode() {
112       final int PRIME = 59;
113       int result = 1;
114       result = (result*PRIME(this.getName() == null this.getName().hashCode());
115       result = (result*PRIME(this.getValue() == null this.getValue().hashCode());
116       return result;
117     }
118   }
119 }
 
 
 
 
So with project lombok you can concentrate on the real code, and the annotations do expand on build to the boilerplate code.
There are many options in lombok to also generate other things for java classes, be sure to look at the documentation.

There is just one "bad" thing about it:
By definition annotations should not create java code, but in this case I think it is worth the "break" of rules.

Thursday, August 28, 2014

Moving files into the azure cloud

Microsoft azure cloud provides SMB 2.1 file server functionality.

This is great when you need to access your files from different VM or services,
since don't need to run your own VM with the fileserver role installed.

As a bonus, you can access your files also via REST service, so your are very flexible in the usage of the service.

A introduction to the fileserver functionality can be found in this blog post.

When you have setup your service correctly, it's time to move some data to it.
The simplest approach would be to use xcopy or a similar tool to upload your files into the azure file service.

The drawback of such standard tools is, that they copy the files sequencially, one by one.
Azure is a cloud which provides massive paralell operations, so there is a faster way to get your file into the cloud.

Microsoft has released a tool named AzCopy which allows you to upload whole folders in paralell, which will speedup your uploads.

In this blog post there are more examples and use cases to bring your files into the Azure cloud.

Thursday, August 21, 2014

Using "related" fields/properties in vaadin tables with JPA containers

Vaadin is a Java framework for building modern web applications that look great, perform well and make you and your users happy.

It has a lot of features which help you in building data driven applications, without having to code everything yourself.
As with any powerfull frameworks you often come to a plcae where a "simple" thing isn't that simple to implement.
Perhaps the framework is just not prepared for that simple feature you wish to use, or you don't find the way to use it correctly.

Vaadin has containers which allow you do present data in forms and tables, without needing to code everything yourself.
There exist different types of containers, depending on your original data source, for example you can have SQL database, Bean objects and many others as a source.

With the JPA container, you can use to handle then whole data stuff with JPA.
So you could for example use hibernate or eclispelink to back your java objects in a sql database.

There is a whole chapter in the book of vaadin describing the JPA container.
When you have a object which has relations to other objects, then you can also specify the JPA container about such "related" properties or fields.
For example when you have a Person object, which has a relation to a country object, you can teel the JPA container about the additional fields available from the Country object.

// Have a persistent container
JPAContainer<Person> persons =
    JPAContainerFactory.make(Person.class, "book-examples");

// Add a nested property to a many-to-one property
persons.addNestedContainerProperty("country.name");
        
// Show the persons in a table, except the "country" column,
// which is an object - show the nested property instead
Table personTable = new Table("The Persistent People", persons);
personTable.setVisibleColumns(new String[]{"name","age",
                                           "country.name"});


// Have a nicer caption for the country.name column
personTable.setColumnHeader("country.name", "Nationality");


The Vaadin JPA container automagically knows to go via the object/database relation and retrieve the correct values.

When you use the filtering table add on available from the vaadin add ons, you can also implement filters on these additional fields.

For this you have to implement the FilterGenerator interface and then tell the table which properties are handled with this filter.
Of course your filter code must then generate the correct filter criterias.

@Override
public Container.Filter generateFilter(Object propertyId, Object value)
{
    if ("country.name".equals(propertyId))
    {
        if (value != null && value instanceof String)
        {
            return new Like("name", value.toString()+"%");
        }
    }
}


Tuesday, August 19, 2014

Free eBook about Ethernet Layer 2 encryption

You can download the free  2014 edition of Layer 2 Ethernet by Christoph Jaggi (published by inside-IT.ch) for your devices.
The book is best viewed on a tablet or computer.
Though you can tap them to expand to full size, there are many graphics which may be difficult to read on smaller devices, such as smartphones.

Go to the download page

For more about Christoph Jaggi, visit www.uebermeister.com or send an email.

Friday, July 11, 2014

130 free ebooks available from Microsoft

Microsoft has many many free ebooks available for download on their different websites.
Up to now it was difficult to find them, since they are scattered over the different sites and pages.

Thanks to Eric Ligman we now have a (complete?) list of all available free ebooks from microsoft.
The books are available in different formats, most are pdf, some are epub or mobi and even some word and zip documents are available.
Some of the ebooks are also available in multiple formats, just look at them.



The books cover MS related topics, from Windows 7, to Server 2012, Sharepoint, SQL Server and many other related topis.

You can also get a free info app, where new free ebooks will be announced.
Unfortunally this one is only available for Windows Phone...

Wednesday, July 2, 2014

What rectangular selection can be used for in Netbeans

Netbeans has very powerfull editing capabilities.
One of the less known features is the regular selection in text files.

This is very handy, when you for example need to remove the first two columns inside a text file.

Go to the first line, press CTRL+SHIFT+R and move the cursor down to then end and left two characters.
This selects the first two character in each line.


Now with a single DEL key you can remove them all.

But there is more to this rectangular stuff...
With the rectangular selection still active, start typing hello

Here what you get...


As you see, not only deletion of text can be made via rectangular selection, but also inserting new text on multiple lines at the same time.

This is handy, when you for example wish to add a css class to a lot of <div> tags at the idention level.

A improved version of this multi row editing capability is sheduled for a upcomming version of netbeans. The corresponding enhancement request can be found here.

Official Office 365 Roadmap available

Microsoft recently published a roadmap for the office 365 environment.
You can look at it here.

It shows what has already been rolled out, what is currently in the rolling out phase and what is intended to be implemented/changed in the next weeks/months.

New features are not released for all office 365 at the same time.
Rather the new functionality is rolled out in several phases, during a few days up to a few months, depending on the impact of the changes.