]> source.dussan.org Git - vaadin-framework.git/commitdiff
Make DefaultFieldGroupFieldFactory singleton (#11038).
authorDenis Anisimov <denis@vaadin.com>
Sun, 21 Sep 2014 16:32:13 +0000 (19:32 +0300)
committerHenri Sara <hesara@vaadin.com>
Mon, 13 Oct 2014 12:20:05 +0000 (12:20 +0000)
Change-Id: I40250fadfe78d663d1cb7614f4f8818216e773a4

server/src/com/vaadin/data/fieldgroup/DefaultFieldGroupFieldFactory.java
server/src/com/vaadin/data/fieldgroup/FieldGroup.java
server/tests/src/com/vaadin/data/DefaultFieldGroupFieldFactoryTest.java

index b1bf58199a8bb5d87b75b90fb0c8f63e8e6ecf09..9c2e4b2f83f2313ffe03f2de8a74db157bfa5626 100644 (file)
@@ -36,10 +36,26 @@ import com.vaadin.ui.RichTextArea;
 import com.vaadin.ui.Table;
 import com.vaadin.ui.TextField;
 
+/**
+ * This class contains a basic implementation for {@link FieldGroupFieldFactory}
+ * .The class is singleton, use {@link #get()} method to get reference to the
+ * instance.
+ * 
+ * @author Vaadin Ltd
+ */
 public class DefaultFieldGroupFieldFactory implements FieldGroupFieldFactory {
 
+    private static final DefaultFieldGroupFieldFactory INSTANCE = new DefaultFieldGroupFieldFactory();
+
     public static final Object CAPTION_PROPERTY_ID = "Caption";
 
+    protected DefaultFieldGroupFieldFactory() {
+    }
+
+    public static DefaultFieldGroupFieldFactory get() {
+        return INSTANCE;
+    }
+
     @Override
     public <T extends Field> T createField(Class<?> type, Class<T> fieldType) {
         if (Enum.class.isAssignableFrom(type)) {
index 27515d15afaf88dd5994a34c9da21a5cdca23f46..c5aab5a0534fd714b5c25c36ce9a43031bf3d841 100644 (file)
@@ -68,7 +68,8 @@ public class FieldGroup implements Serializable {
     /**
      * The field factory used by builder methods.
      */
-    private FieldGroupFieldFactory fieldFactory = new DefaultFieldGroupFieldFactory();
+    private FieldGroupFieldFactory fieldFactory = DefaultFieldGroupFieldFactory
+            .get();
 
     /**
      * Constructs a field binder. Use {@link #setItemDataSource(Item)} to set a
index a5fee5175e694e3624624b1d82afd60d0bf027d1..bbf74bfb21d7fa334ebebb5b6ce83b52acd656ed 100644 (file)
@@ -15,6 +15,7 @@
  */
 package com.vaadin.data;
 
+import java.lang.reflect.Constructor;
 import java.util.Date;
 
 import org.junit.Assert;
@@ -34,7 +35,30 @@ public class DefaultFieldGroupFieldFactoryTest {
 
     @Before
     public void setupFieldFactory() {
-        fieldFactory = new DefaultFieldGroupFieldFactory();
+        fieldFactory = DefaultFieldGroupFieldFactory.get();
+    }
+
+    @Test
+    public void noPublicConstructor() {
+        Class<DefaultFieldGroupFieldFactory> clazz = DefaultFieldGroupFieldFactory.class;
+        Constructor<?>[] constructors = clazz.getConstructors();
+        Assert.assertEquals(
+                "DefaultFieldGroupFieldFactory contains public constructors",
+                0, constructors.length);
+    }
+
+    @Test
+    public void testSameInstance() {
+        DefaultFieldGroupFieldFactory factory1 = DefaultFieldGroupFieldFactory
+                .get();
+        DefaultFieldGroupFieldFactory factory2 = DefaultFieldGroupFieldFactory
+                .get();
+        Assert.assertTrue(
+                "DefaultFieldGroupFieldFactory.get() method returns different instances",
+                factory1 == factory2);
+        Assert.assertNotNull(
+                "DefaultFieldGroupFieldFactory.get() method returns null",
+                factory1);
     }
 
     @Test