Posts

Showing posts with the label example

Java Enum in details

Java 5 has brought linguistic support for enumerated types. An enum type is a type whose fields consist of a fixed set of constants. The enum provides following features Typesafe Namespace Printable constants values Typical and often enum is being used to replace int constants. For example constants for color codes public static final int RED = 0; public static final int GREEN = 1; public static final int BLUE = 3; public static final int WHITE =4; public static final int BLACK = 6;   This code can be replaced by following enum public enum StandardColor { RED,GREEN,BLUE,WHITE,BLACK } Given the above enum declared, we can use the same to communicate the color preferences. For example we have Window class and it supports setting background color using setBackgroundColor(StandardColor color) . One can call setBackgroundColor method on Window's instance to set background color, as shown in code below public class Wind...

WeakHashMap by example

Usually we read following explanation about java.util.WeakHashMap “A hashtable-based Map implementation with weak keys. An entry in a WeakHashMap will automatically be removed when its key is no longer in ordinary use” Unlike java.util.HashMap, WeakHashMap maintain weak reference for the all keys objects and strong reference of value objects. As per features of weak reference, they are more prone to be collection by GC. WeakHashMap runs some kind of unknown services to remove all the entries from collection whose keys are already garbage collected. It means entries in weak hash map may changes over the time. Whenever there  is a need of more memory to your application, GC cycle will execute and its job is to collect all un-referenced objects or weak references. In some cases GC may collected keys of weak hash map as those are a weak references. Once keys are garbage collected , WeakHashMap’s implementation job is to clear all such entries.   See the following program, I...

Eclipse Plug-in Development:Working with Project Facet

Image
Every project may have one or more natures. Each nature brings set of properties to the project.For example a Java project has “src” directory and “bin” as build directory or you may want to associate a set of libraries to your custom project. Faceted Project Framework provides a powerful mechanism for extending the capabilities of the Web Tools Platform. Project facets are typically used as a way of adding functionality to a project. When a facet is added to the project it can perform any necessary setup actions such as copying resources, installing builders, adding natures, etc. Facets can also be used as markers for enabling user interface elements. This post explains defining a facet and using it. Prerequisites : Install WST eclipse plug-in Add dependencies to MANIFEST.MF Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: Sample Bundle-SymbolicName: com.facet.sample; singleton:=true Bundle-Version: 1.0.0 .qualifier Bundle-Activator: com .facet .sample .Activato...