BUG-8043: eliminate ConstraintFactory 43/62443/3
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 29 Aug 2017 08:28:56 +0000 (10:28 +0200)
committerRobert Varga <nite@hq.sk>
Tue, 5 Sep 2017 12:18:06 +0000 (12:18 +0000)
This factory is getting in the way and it really is a useless abstraction,
as we can achieve the same with simple subclass contract.

Change-Id: I3fb50c4bcce08ec79ac710d2ca0bb9e4292ffc53
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/AbstractConstraintEffectiveStatement.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/AbstractListConstraintEffectiveStatement.java [new file with mode: 0644]
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/LengthEffectiveStatementImpl.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/PatternEffectiveStatementImpl.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/RangeEffectiveStatementImpl.java

index ff444a670f629d25ad0f4e7c62ffb7a3958706ad..3a6a674cf372b171542ddff73d5c4ef3395212e7 100644 (file)
@@ -7,16 +7,10 @@
  */
 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type;
 
-import com.google.common.collect.ImmutableList;
-import java.util.ArrayList;
-import java.util.List;
 import org.opendaylight.yangtools.yang.model.api.ConstraintMetaDefinition;
 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
-import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
 import org.opendaylight.yangtools.yang.model.api.type.ModifierKind;
-import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
-import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.DeclaredEffectiveStatementBase;
 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.DescriptionEffectiveStatementImpl;
@@ -34,8 +28,7 @@ abstract class AbstractConstraintEffectiveStatement<A, D extends DeclaredStateme
     private final ModifierKind modifier;
     private final A constraints;
 
-    public AbstractConstraintEffectiveStatement(final StmtContext<A, D, ?> ctx,
-            final ConstraintFactory<A> constraintFactory) {
+    public AbstractConstraintEffectiveStatement(final StmtContext<A, D, ?> ctx) {
         super(ctx);
         String descriptionInit = null;
         String referenceInit = null;
@@ -66,7 +59,7 @@ abstract class AbstractConstraintEffectiveStatement<A, D extends DeclaredStateme
         this.errorAppTag = errorAppTagInit;
         this.errorMessage = errorMessageInit;
         this.modifier = modifierInit;
-        this.constraints = constraintFactory.createConstraints(this, super.argument());
+        this.constraints = createConstraints(super.argument());
     }
 
     @Override
@@ -102,63 +95,7 @@ abstract class AbstractConstraintEffectiveStatement<A, D extends DeclaredStateme
     public final String getErrorMessage() {
         return errorMessage;
     }
-}
-
-abstract class ConstraintFactory<A> {
-    abstract protected A createConstraints(AbstractConstraintEffectiveStatement<A, ?> stmt, A argument);
-}
-
-abstract class ListConstraintFactory<A> extends ConstraintFactory<List<A>> {
-    @Override
-    protected List<A> createConstraints(final AbstractConstraintEffectiveStatement<List<A>, ?> stmt,
-            final List<A> argument) {
-        if (!stmt.isCustomizedStatement()) {
-            return ImmutableList.copyOf(argument);
-        }
 
-        final List<A> customizedConstraints = new ArrayList<>(argument.size());
-        for (final A constraint : argument) {
-            customizedConstraints.add(createCustomizedConstraint(constraint, stmt));
-        }
-        return ImmutableList.copyOf(customizedConstraints);
-    }
-
-    abstract protected A createCustomizedConstraint(A constraint, AbstractConstraintEffectiveStatement<List<A>, ?> stmt);
+    abstract A createConstraints(A argument);
 }
 
-final class LengthConstraintFactory extends ListConstraintFactory<LengthConstraint> {
-    @Override
-    protected LengthConstraint createCustomizedConstraint(final LengthConstraint lengthConstraint,
-            final AbstractConstraintEffectiveStatement<List<LengthConstraint>, ?> stmt) {
-        return new LengthConstraintEffectiveImpl(lengthConstraint.getMin(), lengthConstraint.getMax(),
-                stmt.getDescription(), stmt.getReference(), stmt.getErrorAppTag(), stmt.getErrorMessage());
-    }
-}
-
-final class RangeConstraintFactory extends ListConstraintFactory<RangeConstraint> {
-    @Override
-    protected RangeConstraint createCustomizedConstraint(final RangeConstraint rangeConstraint,
-            final AbstractConstraintEffectiveStatement<List<RangeConstraint>, ?> stmt) {
-        return new RangeConstraintEffectiveImpl(rangeConstraint.getMin(), rangeConstraint.getMax(),
-                stmt.getDescription(), stmt.getReference(), stmt.getErrorAppTag(), stmt.getErrorMessage());
-    }
-}
-
-final class PatternConstraintFactory extends ConstraintFactory<PatternConstraint> {
-    @Override
-    protected PatternConstraint createConstraints(
-            final AbstractConstraintEffectiveStatement<PatternConstraint, ?> stmt, final PatternConstraint argument) {
-        if (!stmt.isCustomizedStatement()) {
-            return argument;
-        }
-
-        return createCustomizedConstraint(argument, stmt);
-    }
-
-    private static PatternConstraint createCustomizedConstraint(final PatternConstraint patternConstraint,
-            final AbstractConstraintEffectiveStatement<?, ?> stmt) {
-        return new PatternConstraintEffectiveImpl(patternConstraint.getRegularExpression(),
-                patternConstraint.getRawRegularExpression(), stmt.getDescription(), stmt.getReference(),
-                stmt.getErrorAppTag(), stmt.getErrorMessage(), stmt.getModifier());
-    }
-}
\ No newline at end of file
diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/AbstractListConstraintEffectiveStatement.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/AbstractListConstraintEffectiveStatement.java
new file mode 100644 (file)
index 0000000..ef7ee9a
--- /dev/null
@@ -0,0 +1,37 @@
+/**
+ * Copyright (c) 2017 Pantheon Technologies, s.r.o. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type;
+
+import com.google.common.collect.ImmutableList;
+import java.util.ArrayList;
+import java.util.List;
+import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
+import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
+
+abstract class AbstractListConstraintEffectiveStatement<T, D extends DeclaredStatement<List<T>>>
+        extends AbstractConstraintEffectiveStatement<List<T>, D> {
+
+    AbstractListConstraintEffectiveStatement(final StmtContext<List<T>, D, ?> ctx) {
+        super(ctx);
+    }
+
+    @Override
+    final List<T> createConstraints(final List<T> argument) {
+        if (!isCustomizedStatement()) {
+            return ImmutableList.copyOf(argument);
+        }
+
+        final List<T> customizedConstraints = new ArrayList<>(argument.size());
+        for (final T constraint : argument) {
+            customizedConstraints.add(createCustomizedConstraint(constraint));
+        }
+        return ImmutableList.copyOf(customizedConstraints);
+    }
+
+    abstract T createCustomizedConstraint(T constraint);
+}
index 19a71c04aeb3453b556f965bb1070eb2ec6191e2..4288fcc0a6bfac176710b9a4dfdc38b6908a9e86 100644 (file)
@@ -13,8 +13,15 @@ import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
 
 public class LengthEffectiveStatementImpl extends
-        AbstractConstraintEffectiveStatement<List<LengthConstraint>, LengthStatement> {
+        AbstractListConstraintEffectiveStatement<LengthConstraint, LengthStatement> {
+
     public LengthEffectiveStatementImpl(final StmtContext<List<LengthConstraint>, LengthStatement, ?> ctx) {
-        super(ctx, new LengthConstraintFactory());
+        super(ctx);
+    }
+
+    @Override
+    final LengthConstraint createCustomizedConstraint(final LengthConstraint lengthConstraint) {
+        return new LengthConstraintEffectiveImpl(lengthConstraint.getMin(), lengthConstraint.getMax(),
+                getDescription(), getReference(), getErrorAppTag(), getErrorMessage());
     }
 }
\ No newline at end of file
index eeb4d4c3baab732be8ea2a7009f5f47cf5f2c486..df6331fedc4804c8d943a03c988c03e79e9dfe48 100644 (file)
@@ -14,6 +14,16 @@ import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
 public class PatternEffectiveStatementImpl extends
         AbstractConstraintEffectiveStatement<PatternConstraint, PatternStatement> {
     public PatternEffectiveStatementImpl(final StmtContext<PatternConstraint, PatternStatement, ?> ctx) {
-        super(ctx, new PatternConstraintFactory());
+        super(ctx);
     }
-}
\ No newline at end of file
+
+    @Override
+    protected PatternConstraint createConstraints(final PatternConstraint argument) {
+        if (!isCustomizedStatement()) {
+            return argument;
+        }
+
+        return new PatternConstraintEffectiveImpl(argument.getRegularExpression(), argument.getRawRegularExpression(),
+            getDescription(), getReference(), getErrorAppTag(), getErrorMessage(), getModifier());
+    }
+}
index dec4c9ed22a65fa9c70d5eb8ef59f2d2a49603ec..3ffe1d672627bec8e2c00feec70bd1e58086a30a 100644 (file)
@@ -13,8 +13,14 @@ import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
 
 public class RangeEffectiveStatementImpl extends
-        AbstractConstraintEffectiveStatement<List<RangeConstraint>, RangeStatement> {
+        AbstractListConstraintEffectiveStatement<RangeConstraint, RangeStatement> {
     public RangeEffectiveStatementImpl(final StmtContext<List<RangeConstraint>, RangeStatement, ?> ctx) {
-        super(ctx, new RangeConstraintFactory());
+        super(ctx);
+    }
+
+    @Override
+    final RangeConstraint createCustomizedConstraint(final RangeConstraint rangeConstraint) {
+        return new RangeConstraintEffectiveImpl(rangeConstraint.getMin(), rangeConstraint.getMax(),
+                getDescription(), getReference(), getErrorAppTag(), getErrorMessage());
     }
 }