Saturday, May 7, 2016

Overriding methods in swift is neat

Swift is really very powerful language, but for developers coming from other OO languages it might be a bit tricky sometimes (for example: absence of abstract classes) and sometimes has neat features for which developers usually don't know util they come to the problem. One of such things is method overriding - it works the same way for procedures (void)  and functions (return value). For easier demonstration purposes in playgrounds functions will be used.
Methods can be distinguished as follows:
instance methods: can be called on instance. Unless the method has final keyword in the declaration it can be overridden in the child class (subclass).
static methods: can be called on class. This methods are internally marked as final, so they cannot be overridden by the child class (subclass).
class methods: can be called on class. Unless the method has final keyword in the declaration it can be overridden in the child class (subclass).
class Parent {

 // Multiplies input parameter by 100
 func instanceFunction(parameter: Int) -> Int {
  return parameter * 100
 }

 // Subscracts from input parameter 10 (class function)
 static func staticFunction(parameter: Int) -> Int {
  return parameter - 10
 }

 // Multiplies parameters with itself
 class func classFunction(parameter: Int) -> Int {
  return parameter * parameter
 }
}

class Child : Parent {

 // Multiplies input parameter by 100 (call to parent) and substracts 100
 override func instanceFunction(parameter: Int) -> Int {
  let result = super.instanceFunction(parameter)
  return result - 100
 }

 // Static functions are final so this is not possible - solution?
 /*override static func staticFunction(parameter: Int) -> Int {
  return result - 9
 }*/

 // Multiplies parameter with itself and substracts 1
 override class func classFunction(parameter: Int) -> Int {
  let result = super.classFunction(10)
  return result - 1
 }
}

// Instance functions override
let parent = Parent()
let child = Child()

print(parent.instanceFunction(10))
print(child.instanceFunction(10))

// Static functions override
print(Parent.staticFunction(10))
print(Child.staticFunction(10)) // Call the same implementation ...

// Class functions override
print(Parent.classFunction(10))
print(Child.classFunction(10))


There are interesting consequences of the example code from above:
  • to prevent overriding of instance or class (marked with class) methods use final keyword
  • class method marked as final behaves the same way as static method
  • when using OO principles it happens many times that we override method of parent class. In the overridden method (in child) so often also call method of parent (super-class). The interesting thing is, that this can be done for instance and class methods by using the same reserved word - super. The only thing I am asking myself is, why having static and class methods, if the same behaviour can be achieved by using class methods...
The possibility of overriding class methods is Swift's very neat feature - it is not supported in all popular languages, for example in Java. In Java we have only static methods and static method with the same name can be defined in both parent and subclass, but this is not real overriding. It is also not possible to invoke method of the parent class via super (for static methods only) without hardcoding the class name. Please note: Java has tremendously powerful reflection API and via it - it is possible to solve this problem, but this is not something what you would like to use on daily basis :) 
public class Parent {
  
  public static int staticFunction(int parameter) {
    
    return parameter * parameter;
  }
}

public class Child extends Parent {
    
  public static int staticFunction(int parameter) {
    
    int result = Parent.staticFunction(parameter);
    return result - 1;
  }
}

Monday, March 28, 2016

Embed UITableViewController into another UIViewController using Storyboard

I have been recently asked to implement side menu with static cells using TableView* + adding some elements on the screen. My initial implementation idea was using to add TableView with other UI components in UIViewController. It turned out that using TableView (not the TableViewController) does not support static cells (the xCode complains - the project does not even compile). So the UITableViewController must be used. 
Actually the solution is very simple, but you must be aware of it:
  1. implement your screen where the TableViewController should be - add any other UI elements to the screen. For the place where you want to have the UITableViewController search for ContainerView and add it to the screen (feel free to add any auto-layout, constraints there)
  2. add UITableViewController to the storyboard - next to you ViewController. Configure it as you wish
  3. select ContainerView, hold CTRL + click and create segue to the TableViewController. From the dropdown menu choose the embed segue. Run the app and voila - TableViewController is inside the first screen :)
  4. feel free to check the demo here: https://github.com/leondobnik/EmbeedTVCDemo.git
Storyboard solution of embedding
The result - UITableViewController inside UIViewController

Wednesday, August 13, 2014

Web Services and DataTypes, Base64Encoding

I have seen many Web Service implementations where developers created their own classes to convert between different data types or they imported third party library just to base64 encode a bunch of text. Well in case you have to convert between some exotic data types there is no other way, but for the most purposes the javax.xml.bind.DatatypeConverter will be just fine. It has many printXXX and  parseXXX methods - for example:
public static boolean parseBoolean(String lexicalXSDBoolean)
public static String printBoolean(boolean val). 
As documentation says, this class has been available since JAXB 1.0, so you can find it even it Java EE5. May the god bless you if you are still dying with Java 1.4 :).
The DatatypeConverter class has also one very important feature: it "knows" how to base64 encode and decode text - the functions are:
public static byte[] parseBase64Binary(String lexicalXSDBase64Binary)
public static String printBase64Binary(byte[] val)
So with this class you can avoid using  third party libraries (such as Apache Commons) or sun.misc.BASE64Encoder and sun.misc.BASE64Decoder classes which have been deprecated for many years. Not to mention that using deprecated classes is considered as a bad practice. For the end I must mention that JDK 8 have brought us the official base64 encoder class java.util.Base64.
If you strive for the best code possible try to use classes mentioned above and let sun.misc.* class retire. They served us well :).

Saturday, January 18, 2014

GlassFish 4.0 GZIP bug

I have been developing a web application in PrimeFaces. Due limited resources (private project) our testing server (demonstration purposes) is hosted by team member on ADSL line. As you may know the limit of ADSL line is 8 mbit for download and 0.768 mbit for upload. The PrimeFaces application is running good on our development machines, but terribly when accessed on testing GlassFish 4.0 Server from the internet - responses (monitored via Developer tools in Chrome) vary from 1 to 4 seconds, sometimes even worse. When GlassFish is accessed from LAN response time is normal - from 80 to 250 ms. I must mention that page displays only a simple form with 7 JSF p:outputLabel and p:inputText elements.
Clearly 4 seconds isn't acceptable response time for a such simple web page. So I started investigating what was causing it. I used Chrome's DeveloperTools and its Network tab where requests and responses are displayed - the data displayed there spoke for themselves: most of the response time was taken by transfer of CSS and JavaScript files. I also checked request and response headers and figured out that server is not using compression (GZIP) although browser requested it.

GZIP Compression BUG


I have logged in into the GlassFish' administration console, activated GZIP compression, restarted GlassFish and for the god sake my application's GUI felt apart. I opened Chrome's Developer Tools and saw following result:
All responses were successfully received, but there was something wrong with the JavaScript files - a bunch of errors like Uncaught Syntax Error: Unexpected identifier and Uncaught ReferenceError: $ is not defined. My first thought was "PrimeFaces is the suspect", but after deploying on second GlassFish server I found out that it must be something with GlassFish 4 and settings that I changed - compression. So I started checking what caused an error - I clicked onto the first error displayed in the upper picture and look what a mess GlassFish's compression did:
it is a jQuery question mark mania. I started googling about this issue and found the following reported bug: https://java.net/jira/browse/GLASSFISH-20726. The solution is simple: you just have to download file nucleus-grizzly-all.jar attached to the first post and copy it into the glassfish4/glassfish/modules directory, where glassfish4 is your root GlassFish4 installation directory. The link also states that this issue will be fixed in GlassFish 4.1 which is planned for summer 2014.
Let me return to our testing GlassFish 4 server on the ADSL line - enabling GZIP has given server wings. The response time now vary between 150 and 250 ms what is significant improvement. You must also take into the account that server is running on 4 or 5 years old dual core processor, Linux OS and runs MySql database. When testing on development GlassFish results responses are much butter.

Enable GZIP compression on GlassFish 4.0


If you try to GZIP already size optimised / compressed file such as MP3 / MP4 file there will achieve close to zero difference in the file size after compression. Furthermore on some files you can achieve only minor file size change - for example shrink 800 KB PDF to 720 KB. In case of web development we are mostly dealing with different types of text files (XML, JSON, CSS, JavaScript). On these files GZIP works like charm - you can compress 60 MB file into astonishing 378 KB. So in case of 60 MB file you save more than 59 MB of bandwidth - that is a really huge improvement. Especially if you are developing a mobile application where every KB counts (cellular network) GZIP compression is a must. If you want to test how much bandwidth or space you can save check this link and perhaps this to write your own Java test case.
The compression is disabled by default on GlassFish 4.0 server. I do not understand reason for a such decision, because we are in year 2014 and not at the beginning of the stone age. So enable GZIP compression by logging in into GlassFish's administration console (http://localhost:4848 by default) and navigate in the left menu: Configurations / server-config / HTTP Service / HTTP Listeners / http-listener-1 (by default GlassFish has three listeners: one for HTTP, one for HTTPS and one for administration).
Under the heading Edit HTTP Listener click on the Advanced link.
After clicking on Advanced link you can see two new tabs - HTTP and File Cache. Click on the HTTP tab.
Now you can scroll down to the label called Compression. You have dropdown list where you can select the compression method: on (meaning that compression will be used when it is requested via HTTP headers) and force (meaning that compression will be always used). I think the most appropriate setting is on, because you are still compatible with relics which does not support GZIP. Click Save and restart GlassFish4.
There are also other compression settings you might be interested: Compressible Mime Types, Compression Minimum size and No-Compression User Agents - the explanation of these settings is under input fields.
For the end as always: comments, better solutions are welcome.

Tuesday, January 7, 2014

Master Java's close() method

I have been developing a Java EE Web application recently and I have used combination of java.io.InputStream and java.io.OutputStream classes. I had some problems with its close() method, so I did a little research and I found out some interesting things. Let's check them out.
There are also two family classes for working with characters - java.io.Reader and java.io.Writer. All this classes implement one common method (already mentioned) called public void close() throws IOException, which is real piece of art and nightmare, because it must be called to free resources, but it also throws checked IOException, so it requires additional handling. So if you do everything by book this masterpiece would come out:
public static void main(String[] args)
{
    String fileLocation = "/docs/README.txt";
    FileInputStream fis = null;
    
    try
    {
      fis = new FileInputStream(fileLocation);
      
      int read = 0;
      byte[] buffer = new byte[8192];
      
      while((read = fis.read(buffer)) > -1)
      {
        System.out.print(new String(buffer, 0, read));
      }
    }
    
    catch(IOException e)
    {
      Logger.getGlobal().log(Level.SEVERE, "Failed to read file at: " + fileLocation, e);
    }
    
    finally
    {
      try
      {
        if (fis != null)
        {
          fis.close();
        }
      }
      
      catch(IOException e)
      {
        Logger.getGlobal().log(Level.SEVERE, "Failed to close stream reading file at: " + fileLocation, e);
      }
    }
  }
I think it is quite obvious why I call it masterpiece - there is more error handling code than logic. Furthermore imagine the amount of code, if there would be an OutputStream object.
The same problem is with java.sql classes, such as java.sql.Connection, java.sql.PreparedStatement,… So what can we do? Well as research shows, it depends on the version of Java you are running.

JavaSe 7


JavaSe7 introduced java.lang.AutoClosable interface which is implemented by all streaming java.io classes and java.sql classes. The documentation states: Closes this resource, relinquishing any underlying resources. This method is invoked automatically on objects managed by the try-with-resources statement. Nice so the correct code then is:
  public static void main(String[] args)
  {
    String fileLocation = "/docs/README.txt";
    
    try(FileInputStream fis = new FileInputStream(fileLocation);)
    { 
      int read = 0;
      byte[] buffer = new byte[8192];
      
      while((read = fis.read(buffer)) > -1)
      {
        System.out.print(new String(buffer, 0, read));
      }
    }
    
    catch(IOException e)
    {
      Logger.getGlobal().log(Level.SEVERE, "Failed to read file at: " + fileLocation, e);
    }
  } 
Fancy isn't it? Yes it is, but WARNING: if you use this JavaSe7 feature set your project's Source/binary format in IDE to JDK7. Without this, this code may be run on lower versions of Java and wonderful bug is created. Again note, you can use this feature for java.sql classes too.
Update: on 22.06.2014 I noticed a small but important mistake in the example above: a FileInputStream instance (fis) was not inside brackets at try keyword. If you want to use try-with-resources then all AutoClosable instances must be inside brackets at try keyword, as is now correctly displayed in the example above.

JavaSe 5 and JavaSe6


Java 5 and 6 java.io streaming classes implement java.io.Closeable interface. There are two solutions available: an Apache Commons IO library or implementation of custom solution.
The Apache Commons IO library has IOUtils class which has numerous closeQuietly methods implemented - implementation of them is quite simple:
public static void closeQuietly(final OutputStream output)
{
  closeQuietly((Closeable)output);
}

public static void closeQuietly(final Closeable closeable) 
{
  try 
  {
    if (closeable != null)
    {
      closeable.close();
    }
  } 

  catch (final IOException joe)
  {
    // ignore
   }    
  }
}
The Commons IO library is really a handy tool, but there is something to be considered - the catch statement is empty - this bad practice is also known as exception swallowing - well in the presented scenario there is really not much that can be done, but I would at least attach a Logger on the finest/trace level.
I have used many APIs which take InputStream and produce something in the OutputStream. In some cases there was also a need to redirect OutputStream to InputStream via PipedInputStream PipedOutputStream combo - so four closeQuietly calls - no thank you. This is a feature I miss in Apache's library. So let's try to make something for us, lazy developers.
// Package
package ioutils;

// Imported classes
import java.io.Closeable;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * An utility class for working with java.io
 * @author Leon Dobnik
 */
public abstract class IoUtils
{
  /**
   * Closes any java.io.* object which implements java.io.Closeable interface
   * @param closeable closeable object to close
   * @see java.io.Closeable
   */
  public static void closeQuietly(final Closeable closeable)
  {
    if (closeable != null)
    {
      try
      {
        closeable.close();
      }
      
      catch(IOException e)
      {
        Logger.global.log(Level.FINEST, "Failed to close Closeable " + closeable.getClass().getCanonicalName(), e);
      }
    }
  }
  
  /**
   * Closes multiple java.io.* objects that implement java.io.Closeable interface
   * @param closeables to close 
   * @see java.io.Closeable
   */
  public static void closeQuietly(Closeable... closeables)
  {
    for (Closeable closeable : closeables)
    {
      closeQuietly(closeable);
    }
  }
}

 public static void main(String[] args)
  {
    
    String fileLocation = "C:\\source.file";
    String fileDestination = "C:\\destination.file";
    
    FileInputStream fis = null;
    FileOutputStream fos = null;
            
    try
    {
      fis = new FileInputStream(fileLocation);
      fos = new FileOutputStream(fileDestination);
      
      int read = 0;
      byte[] buffer = new byte[8192];
      
      while((read = fis.read(buffer)) > -1)
      {
        fos.write(buffer, 0, read);
      }
      
      fos.flush();
    }
    
    catch(IOException e)
    {
      Logger.global.log(Level.SEVERE, "Failed to read file at: " + fileLocation, e);
    }
    
    IoUtils.closeQuietly(fis, fos); // Nice and shiny close :)
  }
So to make your own IoUtils class you need to implement just one method - closeQuietly(Closeable closeable). If you want to pass an unlimited number of parameters in single call, add an overloaded method  with Cloneable… cloneable parameter. The demonstration function copies one file into another (Note: I know that there are Buffered* Stream classes, but that's not the point of this post).
Unfortunately the Closeable interface in available only for java.io classes, so for java.sql classes you need to write custom closeQuietly methods for each class (java.io.Connection, java.io.Statement, …)

Java Se 1.4 


Well if you still use this ancient relic of the past, ready yourself for the implementation marathon - there is no Closeable interface - so for every class that is to be closed quietly a method called closeQuietly must be implemented (of course you can name it fancyClose or helloKittyClose if you want) - one for InputStream, one for OutputStream and so on.
There is also no ClassType… parameterName feature - but that does not mean that this ancient relic cannot handle features of JavaSe5 and JavaSe6. It can, but solution and use is not so elegant.
// Package
package ioutils;

// Imported classes
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.util.logging.Logger;

/**
 * An utility class for working with java.io
 * @author Leon Dobnik
 */
public abstract class IoUtils
{
  /**
   * Closes java.io.InputStream without throwing exception
   * @param is stream to close
   * @see java.io.InputStream
   */
  public static void closeQuietly(final InputStream is)
  {
    internalCloseQuietly((Object)is);
  }
  
  /**
   * Closes java.io.OutputStream without throwing exception
   * @param os stream to close
   */
  public static void closeQuietly(final OutputStream os)
  {
    internalCloseQuietly((Object)os);
  }
  
  /**
   * Invokes close() method on some java object
   * @param o instance of which close() method may be invoked
   */
  private static void internalCloseQuietly(final Object o)
  {
    if (o != null)
    {
      try
      {
        Method closeMethod = o.getClass().getMethod("close", null);
        
        if (closeMethod != null)
        {
          closeMethod.invoke(o, null);
        }
      }
      
      catch(Exception e) // In real world, catch specific exceptions...
      {
        Logger.global.finest("Failed to call close() method on: " + o.getClass().getName());
      }  
    }  
  }      
  
  /**
   * Closes quietly multiple objects that implement close() method. If object does
   * not have close() method it will be ignored.
   * @param objects 
   */
  public static void closeQuielty(final Object[] objects)
  {
    if (objects != null)
    {  
      for (int i = 0; i < objects.length; i++)
      {
        internalCloseQuietly(objects[i]);
      }
    }  
  }
}
  public static void main(String[] args)
  {
    
    String fileLocation = "C:\\fileSource.txt";
    String fileDestination = "C:\\fileDestination.txt";
    
    FileInputStream fis = null;
    FileOutputStream fos = null;
            
    try
    {
      fis = new FileInputStream(fileLocation);
      fos = new FileOutputStream(fileDestination);
      
      int read = 0;
      byte[] buffer = new byte[8192];
      
      while((read = fis.read(buffer)) > -1)
      {
        fos.write(buffer, 0, read);
      }
      
      fos.flush();
    }
    
    catch(IOException e)
    {
      Logger.global.log(Level.SEVERE, "Failed to read file at: " + fileLocation, e);
    }
    
    IoUtils.closeQuielty(new Object[]{fis, fos}); // Nice and shiny close :)
  }
So in case of this relic a solution is to use Reflection API for invoking close() method on objects and writing public wrapper methods for supported types. The method closeQuietly(final Object[] objects) is an option if you want to close multiple objects in one call, but it is your decision if it meets coding standards of your organisation.

Any comments are welcome.

Monday, October 21, 2013

Hello world!

On this blog you will soon find a lot of interesting things from Java Development world (especially Java EE) and iOS world.