BUG-6522: share instances of Config(Effective)Statement 47/47347/1
authorRobert Varga <rovarga@cisco.com>
Fri, 21 Oct 2016 09:00:15 +0000 (11:00 +0200)
committerRobert Varga <nite@hq.sk>
Fri, 21 Oct 2016 14:16:29 +0000 (14:16 +0000)
Config statement has low cardinality, use specialized classes
for its representation.

Change-Id: I7b5dc7e7069e5e7cfa4808d44a95f147f6b7073d
Signed-off-by: Robert Varga <rovarga@cisco.com>
(cherry picked from commit 2ad7b79d5a61b540afca3ce37ab81738bbb699b7)

yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/ConfigStatementImpl.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/EmptyConfigEffectiveStatement.java [new file with mode: 0644]
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/EmptyConfigStatement.java [new file with mode: 0644]
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/EmptyMandatoryStatement.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/ConfigEffectiveStatement.java [new file with mode: 0644]
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/ConfigEffectiveStatementImpl.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/DeviateEffectiveStatementImpl.java

index af1d8dd42862dfd74c0e9e7346ba92f78e9a5a75..886099e8ebe4155a96cb229e1a88fc4a02be691d 100644 (file)
@@ -39,13 +39,26 @@ public class ConfigStatementImpl extends AbstractDeclaredStatement<Boolean> impl
 
         @Override
         public ConfigStatement createDeclared(final StmtContext<Boolean, ConfigStatement, ?> ctx) {
-            return new ConfigStatementImpl(ctx);
+            final ConfigStatement ret = new ConfigStatementImpl(ctx);
+
+            if (EmptyConfigStatement.FALSE.equals(ret)) {
+                return EmptyConfigStatement.FALSE;
+            } else if (EmptyConfigStatement.TRUE.equals(ret)) {
+                return EmptyConfigStatement.TRUE;
+            } else {
+                return ret;
+            }
         }
 
         @Override
         public EffectiveStatement<Boolean, ConfigStatement> createEffective(
                 final StmtContext<Boolean, ConfigStatement, EffectiveStatement<Boolean, ConfigStatement>> ctx) {
-            return new ConfigEffectiveStatementImpl(ctx);
+            final EffectiveStatement<Boolean, ConfigStatement> ret = new ConfigEffectiveStatementImpl(ctx);
+            final ConfigStatement declared = ret.getDeclared();
+            if (declared instanceof EmptyConfigStatement && ret.effectiveSubstatements().isEmpty()) {
+                return ((EmptyConfigStatement)declared).toEffective();
+            }
+            return ret;
         }
 
         @Override
diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/EmptyConfigEffectiveStatement.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/EmptyConfigEffectiveStatement.java
new file mode 100644 (file)
index 0000000..d5e30e8
--- /dev/null
@@ -0,0 +1,68 @@
+/**
+ * Copyright (c) 2016 Cisco Systems, Inc. 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;
+
+import com.google.common.collect.ImmutableList;
+import java.util.Collection;
+import java.util.Map;
+import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
+import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
+import org.opendaylight.yangtools.yang.model.api.meta.StatementSource;
+import org.opendaylight.yangtools.yang.model.api.stmt.ConfigStatement;
+import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.ConfigEffectiveStatement;
+
+abstract class EmptyConfigEffectiveStatement implements ConfigEffectiveStatement {
+    static final EmptyConfigEffectiveStatement FALSE = new EmptyConfigEffectiveStatement() {
+        @Override
+        public ConfigStatement getDeclared() {
+            return EmptyConfigStatement.FALSE;
+        }
+    };
+
+    static final EmptyConfigEffectiveStatement TRUE = new EmptyConfigEffectiveStatement() {
+        @Override
+        public ConfigStatement getDeclared() {
+            return EmptyConfigStatement.TRUE;
+        }
+    };
+
+    private EmptyConfigEffectiveStatement() {
+        // Hidden
+    }
+
+    @Override
+    public final StatementDefinition statementDefinition() {
+        return getDeclared().statementDefinition();
+    }
+
+    @Override
+    public final Boolean argument() {
+        return getDeclared().argument();
+    }
+
+    @Override
+    public final StatementSource getStatementSource() {
+        return getDeclared().getStatementSource();
+    }
+
+    @Override
+    public final <K, V, N extends IdentifierNamespace<K, V>> V get(final Class<N> namespace, final K identifier) {
+        throw new UnsupportedOperationException("Not implemented yet.");
+    }
+
+    @Override
+    public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAll(final Class<N> namespace) {
+        throw new UnsupportedOperationException("Not implemented yet.");
+    }
+
+    @Override
+    public final Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
+        return ImmutableList.of();
+    }
+}
diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/EmptyConfigStatement.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/EmptyConfigStatement.java
new file mode 100644 (file)
index 0000000..19d0dc2
--- /dev/null
@@ -0,0 +1,99 @@
+/**
+ * Copyright (c) 2016 Cisco Systems, Inc. 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;
+
+import com.google.common.collect.ImmutableList;
+import java.util.Collection;
+import java.util.Objects;
+import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
+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.meta.StatementDefinition;
+import org.opendaylight.yangtools.yang.model.api.meta.StatementSource;
+import org.opendaylight.yangtools.yang.model.api.stmt.ConfigStatement;
+
+abstract class EmptyConfigStatement implements ConfigStatement {
+    static final ConfigStatement FALSE = new EmptyConfigStatement() {
+        @Override
+        public boolean getValue() {
+            return false;
+        }
+
+        @Override
+        EffectiveStatement<Boolean, ConfigStatement> toEffective() {
+            return EmptyConfigEffectiveStatement.FALSE;
+        }
+    };
+
+    static final ConfigStatement TRUE = new EmptyConfigStatement() {
+        @Override
+        public boolean getValue() {
+            return true;
+        }
+
+        @Override
+        EffectiveStatement<Boolean, ConfigStatement> toEffective() {
+            return EmptyConfigEffectiveStatement.TRUE;
+        }
+    };
+
+    private EmptyConfigStatement() {
+        // Invisible on purpose
+    }
+
+    abstract EffectiveStatement<Boolean, ConfigStatement> toEffective();
+
+    @Override
+    public final Collection<? extends DeclaredStatement<?>> declaredSubstatements() {
+        return ImmutableList.of();
+    }
+
+    @Override
+    public final StatementDefinition statementDefinition() {
+        return Rfc6020Mapping.CONFIG;
+    }
+
+    @Override
+    public final String rawArgument() {
+        return Boolean.toString(getValue());
+    }
+
+    @Override
+    public final Boolean argument() {
+        return Boolean.valueOf(getValue());
+    }
+
+    @Override
+    public final StatementSource getStatementSource() {
+        return StatementSource.DECLARATION;
+    }
+
+    @Override
+    public final int hashCode() {
+        return Objects.hash(statementDefinition(), getStatementSource(), argument(),
+            rawArgument(), declaredSubstatements(), getValue());
+    }
+
+    @Override
+    public final boolean equals(final Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (!(obj instanceof ConfigStatement)) {
+            return false;
+        }
+
+        final ConfigStatement other = (ConfigStatement) obj;
+
+        return  getValue() == other.getValue() && argument().equals(other.argument()) &&
+                rawArgument().equals(other.rawArgument()) &&
+                statementDefinition().equals(other.statementDefinition()) &&
+                getStatementSource().equals(other.getStatementSource()) &&
+                declaredSubstatements().equals(other.declaredSubstatements());
+    }
+}
index b83dbd8a286761c63fd7d89df3b97cb41217b621..ff2621c2bf20ca2655cc1ae1fbcee89b37099b23 100644 (file)
@@ -7,8 +7,8 @@
  */
 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020;
 
+import com.google.common.collect.ImmutableList;
 import java.util.Collection;
-import java.util.Collections;
 import java.util.Objects;
 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
@@ -50,7 +50,7 @@ abstract class EmptyMandatoryStatement implements MandatoryStatement {
 
     @Override
     public final Collection<? extends DeclaredStatement<?>> declaredSubstatements() {
-        return Collections.emptyList();
+        return ImmutableList.of();
     }
 
     @Override
diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/ConfigEffectiveStatement.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/ConfigEffectiveStatement.java
new file mode 100644 (file)
index 0000000..7f58ddf
--- /dev/null
@@ -0,0 +1,15 @@
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. 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;
+
+import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.ConfigStatement;
+
+public interface ConfigEffectiveStatement extends EffectiveStatement<Boolean, ConfigStatement> {
+
+}
index 2667dd5fe2295f02de064bb82a89bedda6521783..0de76b721e486b3b473732e19b96fa761789d5c7 100644 (file)
@@ -10,7 +10,8 @@ package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
 import org.opendaylight.yangtools.yang.model.api.stmt.ConfigStatement;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
 
-public final class ConfigEffectiveStatementImpl extends DeclaredEffectiveStatementBase<Boolean, ConfigStatement> {
+public final class ConfigEffectiveStatementImpl extends DeclaredEffectiveStatementBase<Boolean, ConfigStatement>
+        implements ConfigEffectiveStatement {
     public ConfigEffectiveStatementImpl(final StmtContext<Boolean, ConfigStatement, ?> ctx) {
         super(ctx);
     }
index c56f146c010607b0b0de2c57dc26df0c93fc7488..40cc4263cbd87e8cb8e8fd6f8d1a7b88cffba570 100644 (file)
@@ -42,7 +42,7 @@ public final class DeviateEffectiveStatementImpl
 
         this.deviateType = argument();
 
-        final ConfigEffectiveStatementImpl configStmt = firstEffective(ConfigEffectiveStatementImpl.class);
+        final ConfigEffectiveStatement configStmt = firstEffective(ConfigEffectiveStatement.class);
         this.deviatedConfig = configStmt == null ? null : configStmt.argument();
         final DefaultEffectiveStatementImpl defaultStmt = firstEffective(DefaultEffectiveStatementImpl.class);
         this.deviatedDefault = defaultStmt == null ? null : defaultStmt.argument();
@@ -112,7 +112,7 @@ public final class DeviateEffectiveStatementImpl
     }
 
     @Override
-    public boolean equals(Object obj) {
+    public boolean equals(final Object obj) {
         if (this == obj) {
             return true;
         }
@@ -140,4 +140,4 @@ public final class DeviateEffectiveStatementImpl
         return Objects.hash(deviateType, deviatedConfig, deviatedDefault, deviatedMandatory, deviatedMaxElements,
                 deviatedMinElements, deviatedMustDefinitions, deviatedType, deviatedUniqueConstraints, deviatedUnits);
     }
-}
\ No newline at end of file
+}