From dda6e0c4ddf6e4bb4cd777e24ccb22acbb3790a7 Mon Sep 17 00:00:00 2001 From: Denis Anisimov Date: Sun, 21 Sep 2014 19:32:13 +0300 Subject: [PATCH] Make DefaultFieldGroupFieldFactory singleton (#11038). Change-Id: I40250fadfe78d663d1cb7614f4f8818216e773a4 --- .../DefaultFieldGroupFieldFactory.java | 16 ++++++++++++ .../vaadin/data/fieldgroup/FieldGroup.java | 3 ++- .../DefaultFieldGroupFieldFactoryTest.java | 26 ++++++++++++++++++- 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/server/src/com/vaadin/data/fieldgroup/DefaultFieldGroupFieldFactory.java b/server/src/com/vaadin/data/fieldgroup/DefaultFieldGroupFieldFactory.java index b1bf58199a..9c2e4b2f83 100644 --- a/server/src/com/vaadin/data/fieldgroup/DefaultFieldGroupFieldFactory.java +++ b/server/src/com/vaadin/data/fieldgroup/DefaultFieldGroupFieldFactory.java @@ -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 createField(Class type, Class fieldType) { if (Enum.class.isAssignableFrom(type)) { diff --git a/server/src/com/vaadin/data/fieldgroup/FieldGroup.java b/server/src/com/vaadin/data/fieldgroup/FieldGroup.java index 27515d15af..c5aab5a053 100644 --- a/server/src/com/vaadin/data/fieldgroup/FieldGroup.java +++ b/server/src/com/vaadin/data/fieldgroup/FieldGroup.java @@ -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 diff --git a/server/tests/src/com/vaadin/data/DefaultFieldGroupFieldFactoryTest.java b/server/tests/src/com/vaadin/data/DefaultFieldGroupFieldFactoryTest.java index a5fee5175e..bbf74bfb21 100644 --- a/server/tests/src/com/vaadin/data/DefaultFieldGroupFieldFactoryTest.java +++ b/server/tests/src/com/vaadin/data/DefaultFieldGroupFieldFactoryTest.java @@ -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 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 -- 2.39.5