Search

Aman Nepid

Break some rules … Make some noise … Stay Silly … Stay Alive

Singleton Pattern ? Enum ?

Wassup reader. If the title dragged you inside this post; sorry to break you heart. I am not going to explain what is Singleton Pattern, how and why do we use it. Yet I can refer you a good read links as below { yeh .. its free .. knowledge should be free 😉 }

  1. Wikipedia: Singleton Pattern
  2. Tutorialspoint: Singleton Pattern

Still not sure what is the best way to get the singleton behavior out of you object. I am going to demonstrate you a way to achieve Singleton Pattern using Enum.

Please run the below code in your favorite IDE ( mine is Eclipse though).

Singleton.java

public enum Singleton {
INSTANCE;

private String name;

public void setName(String name) {
this.name = name;
}

public String getName() {
return this.name;
}
}

SingletonTest.java

public class SingletonTest {

public static void main(String[] args) {
Singleton firstObj = Singleton.INSTANCE;
firstObj.setName("#coder");

System.out.println("First instance: " + firstObj.getName());

Singleton secondObj = Singleton.INSTANCE;
secondObj.setName("#moon");

System.out.println("Second instance: " + secondObj.getName());
System.out.println("First instance: " + firstObj.getName());
}

}

Output:

First instance: #coder
Second instance: #moon
First instance: #moon

As you can see we get only one instance of Singletion enum, no matter how many we create multiple instance of it. So we achieved the Singleton nature using enum.

Butttttt …  why enum? Isn’t there any other apporach??

Of course there are many but to know why Singleton Pattern is best achieved with Enum please read the following posts carefully and slowly 😉

In short, by using Enum Singleton Pattern we can get Serialized and Lazy Loaded Singleton Object.

Did I forgot to mention enjoy ? 😉

 

 

Discovering #AARRGGBB or your #??RRGGBB in Android

Hey Rockstar, do you know what is the hex value of 75% Black color ? Is it #BF000000? First let me remind you something else than how hex value of 75% black became #BF00000.

Android uses Hex ARGB values, which are formatted as #AARRGGBB. So what is the firs pair AA? I assume that you know #RRGGBB means. Yeah, its simple its the RED, GREEN and BLUE hex color value.

An 8-digit hex color value is a representation of ARGB (Alpha, Red, Green, Blue), whereas a 6-digit value just assumes 100% opacity (fully opaque) and defines just the RGB values. So to make this be fully opaque, you can either use #FF555555, or just #555555. Each 2-digit hex value is one byte, representing values from 0-255.

So how to find the Alpha values in Hex ?

  1. Take your opacity as a decimal value and multiply it by 255. So, if you have a block that is 50% opaque the decimal value would be .5. For example .5 * 255 = 127.5
  2. The fraction won’t convert to hex, so you must round your number up or down to the nearest whole number. For example 127.5 rounds up to 128; 55.25 rounds down to 55.
  3. Enter you decimal value in a decimal to hexadecimal converter, like this:
    http://www.binaryhexconverter.com/decimal-to-hex-converter and convert your values.
  4. If you only get back a single value, prefix it with a zero. For example, if you’re trying to get 5% opacity and you are going through this process you’ll end up with the hex value of D. Add a zero in front of it so it appears 0D.

That’s how you find the alpha channel value. But still are you lazy enough to just copy and paste the values? Thank me for the below list of values 😉

100% — FF
    95% — F2
    90% — E6
    85% — D9
    80% — CC
    75% — BF
    70% — B3
    65% — A6
    60% — 99
    55% — 8C
    50% — 80
    45% — 73
    40% — 66
    35% — 59
    30% — 4D
    25% — 40
    20% — 33
    15% — 26
    10% — 1A
    5% — 0D
    0% — 00

Oh I almost forgot if you are an energetic android developer than below piece of code will give you the Alpha channel values in hex. Try this:

public class AndroidColorsAlphaChannel {
public static void main(String[] args) {
for (double i = 1; i >= 0; i -= 0.01) {
 i = Math.round(i * 100) / 100.0d;
 int alpha = (int) Math.round(i * 255);
 String hex = Integer.toHexString(alpha).toUpperCase();
 if (hex.length() == 1) {
 hex = "0" + hex;
 }
 int percent = (int) (i * 100);
 System.out.println(String.format("%d%% — %s", percent, hex));
 }
 }
}

Did I forgot to mention enjoy ? 😉

Be Aware

It is the first attempt to abstract art kind of mine. It is done during a boring team meeting at my office.

Every newborn and about to born should be aware that this world is a mesh.

image
Be Aware #1

Get KeyHash from Android App

While developing for the Android Application we may need to generate our System KeyHash. Sometimes it may be difficult or wrong if we miss any of the precise instruction. So code below will show your system KeyHash in LogCat View:

try {
     PackageInfo info = getPackageManager().getPackageInfo("your.package.name",PackageManager.GET_SIGNATURES);

     for (Signature signature : info.signatures) {
         MessageDigest md = MessageDigest.getInstance("SHA");
         md.update(signature.toByteArray());
         Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
     }
} catch (NameNotFoundException e) { 

} catch (NoSuchAlgorithmException e) {

}
[/code]

Save your changes and re-run your application. Check your LogCat output for a message similar to this:

12-20 10:47:37.747: D/KeyHash:(936): gpjJPSisErdsl65a1wOqJ+Bpw14=

The bold text above is the KeyHas you needed. 

FYI: I have used above code to get the KeyHash for Facebook integration in Android. So I am quiet not sure about other usage, but still you gotta try 😛

Enjoy 😉

Sunrise seen from the Road at Lalitpur.
Sunrise seen from the Road at Lalitpur.

Leapfrog @ 7 A.M.

Experience with ZK

I find ZK as a quick learning and very much flexible framework with lots of references and code snippets.

It is helping me to develop an enterprise level application in my office. Very much thanks to the ZK Team.

And i refer this framework to the learner Java Programmer who want to hit their hands on RIA’s within short period of time.

Create a free website or blog at WordPress.com.

Up ↑