diff --git a/com.archimatetool.model/src/com/archimatetool/model/impl/Feature.java b/com.archimatetool.model/src/com/archimatetool/model/impl/Feature.java index 8ad51cf84..c4946850d 100644 --- a/com.archimatetool.model/src/com/archimatetool/model/impl/Feature.java +++ b/com.archimatetool.model/src/com/archimatetool/model/impl/Feature.java @@ -220,5 +220,20 @@ public String toString() { result.append(')'); return result.toString(); } + + /** + * Over-ride this to test equality based on this Feature's name + * This ensures we have unique entries keyed by name + */ + @Override + public boolean equals(Object obj) { + if(this == obj) { + return true; + } + + return (obj instanceof Feature) && + name != null && + name.equals(((Feature)obj).name); + } } //Feature diff --git a/com.archimatetool.model/src/com/archimatetool/model/impl/FeaturesEList.java b/com.archimatetool.model/src/com/archimatetool/model/impl/FeaturesEList.java index 80da81696..84741e735 100644 --- a/com.archimatetool.model/src/com/archimatetool/model/impl/FeaturesEList.java +++ b/com.archimatetool.model/src/com/archimatetool/model/impl/FeaturesEList.java @@ -35,12 +35,6 @@ public IFeature putString(String name, String value, String defaultValue) { IFeature feature = getFeature(name); - // Value equals default value so remove it - if(value.equals(defaultValue)) { - remove(name); - return feature; - } - // New one if(feature == null) { feature = IArchimateFactory.eINSTANCE.createFeature(); @@ -48,7 +42,11 @@ public IFeature putString(String name, String value, String defaultValue) { feature.setValue(value); add(feature); } - // Different + // Value equals default value so remove it + else if(value.equals(defaultValue)) { + remove(feature); + } + // Different value else if(!value.equals(feature.getValue())) { feature.setValue(value); } @@ -134,4 +132,13 @@ private void checkNull(String s) { throw new IllegalArgumentException("key or value cannot be null"); //$NON-NLS-1$ } } + + /** + * Return true so that {@link #contains(Object)} checks {@link Feature#equals(Object)} + * For unique entries keyed by name + */ + @Override + protected boolean useEquals() { + return true; + } } diff --git a/tests/com.archimatetool.model.tests/src/com/archimatetool/model/impl/FeaturesEListTests.java b/tests/com.archimatetool.model.tests/src/com/archimatetool/model/impl/FeaturesEListTests.java index 6b6305660..930d9cf0e 100644 --- a/tests/com.archimatetool.model.tests/src/com/archimatetool/model/impl/FeaturesEListTests.java +++ b/tests/com.archimatetool.model.tests/src/com/archimatetool/model/impl/FeaturesEListTests.java @@ -162,4 +162,92 @@ public void getFeature() { assertNull(list.getFeature("bogus")); } + + @Test + public void noDuplicateFeature1() { + IFeature feature = IArchimateFactory.eINSTANCE.createFeature(); + feature.setName("uniqueName"); + feature.setValue("someValue"); + + // Add the feature + boolean result = list.add(feature); + assertTrue(result); + + // Should not be able to add the first feature again + result = list.add(feature); + assertFalse(result); + + // Should not be able to add the first feature again, even if name and value change + feature.setName("uniqueName2"); + feature.setValue("someValue2"); + result = list.add(feature); + assertFalse(result); + } + + @Test + public void noDuplicateFeature2() { + IFeature feature = IArchimateFactory.eINSTANCE.createFeature(); + feature.setName("uniqueName"); + feature.setValue("someValue"); + + // Add the feature + boolean result = list.add(feature); + assertTrue(result); + + // Should not be able to add a different feature object with the same name + IFeature feature2 = IArchimateFactory.eINSTANCE.createFeature(); + feature2.setName("uniqueName"); + feature2.setValue("someValue2"); + result = list.add(feature2); + assertFalse(result); + } + + @Test + public void noDuplicateFeature3() { + IFeature feature = IArchimateFactory.eINSTANCE.createFeature(); + feature.setName("uniqueName"); + feature.setValue("someValue"); + + // Add the feature + boolean result = list.add(feature); + assertTrue(result); + + // Should be able to add a different feature object with a different name + IFeature feature2 = IArchimateFactory.eINSTANCE.createFeature(); + feature2.setName("uniqueName2"); + feature2.setValue("someValue2"); + result = list.add(feature2); + assertTrue(result); + } + + @Test + public void noDuplicateFeature4() { + // Create another list + IArchimateElement element = IArchimateFactory.eINSTANCE.createArtifact(); + FeaturesEList list2 = new FeaturesEList(IFeature.class, (InternalEObject)element, IArchimatePackage.ARCHIMATE_CONCEPT__FEATURES); + + // Add Feature to first list + IFeature feature1 = IArchimateFactory.eINSTANCE.createFeature(); + feature1.setName("uniqueName"); + feature1.setValue("someValue"); + list.add(feature1); + + // Add feature with same name to second list + IFeature feature2 = IArchimateFactory.eINSTANCE.createFeature(); + feature2.setName("uniqueName"); + feature2.setValue("someValue"); + list2.add(feature2); + + // Should not be able to add first list to second list + boolean result = list2.addAll(list); + assertFalse(result); + + // remove it + list2.remove(feature2); + + // Should be able to add it now + result = list2.addAll(list); + assertTrue(result); + } + }