Effective Java 2nd Edition:
authorgumartinm <gu.martinm@gmail.com>
Wed, 12 Oct 2011 17:50:05 +0000 (19:50 +0200)
committergumartinm <gu.martinm@gmail.com>
Wed, 12 Oct 2011 17:50:05 +0000 (19:50 +0200)
Item 27

Change-Id: Idc2ff6e5e807e32961a9da4e7531b8133c7917c0
Signed-off-by: gumartinm <gu.martinm@gmail.com>
.gitignore [new file with mode: 0644]
EffectiveJava2nd/Item27/src/test/SingletonFactory.java [new file with mode: 0644]
EffectiveJava2nd/Item27/src/test/UnaryFunction.java [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..cc9a220
--- /dev/null
@@ -0,0 +1,3 @@
+bin/
+.classpath
+.project
diff --git a/EffectiveJava2nd/Item27/src/test/SingletonFactory.java b/EffectiveJava2nd/Item27/src/test/SingletonFactory.java
new file mode 100644 (file)
index 0000000..2b195eb
--- /dev/null
@@ -0,0 +1,39 @@
+/**
+ * 
+ */
+package test;
+
+/**
+ * @author gusarapo
+ *
+ */
+public class SingletonFactory {
+       
+       private static UnaryFunction<Object> IDENTITY_FUNCTION =
+                       new UnaryFunction<Object>() {
+                               public Object apply(Object arg)
+                               {
+                                       return arg;
+                               }
+       };      
+       /**
+        * @param args
+        */
+       public static void main(String[] args) {
+               String[] strings = {"jute", "hemp", "nylon"};
+               UnaryFunction<String> sameString = identityFunction();
+               for (String s : strings)
+                       System.out.println(sameString.apply(s));
+               
+        Number [] numbers = {1, 2.0, 3L };
+               UnaryFunction<Number> sameNumber = identityFunction();
+               for (Number n : numbers)
+                       System.out.println(sameNumber.apply(n));
+       }
+       
+       @SuppressWarnings("unchecked")
+       public static <T> UnaryFunction<T> identityFunction() {
+               return (UnaryFunction<T>) IDENTITY_FUNCTION;
+       }
+
+}
diff --git a/EffectiveJava2nd/Item27/src/test/UnaryFunction.java b/EffectiveJava2nd/Item27/src/test/UnaryFunction.java
new file mode 100644 (file)
index 0000000..e212f1e
--- /dev/null
@@ -0,0 +1,12 @@
+/**
+ * 
+ */
+package test;
+
+/**
+ * @author gustavo
+ *
+ */
+public interface UnaryFunction<T> {
+       T apply (T arg);
+}