Skip to content

Commit

Permalink
[Features] Allow a null value
Browse files Browse the repository at this point in the history
  • Loading branch information
Phillipus committed Sep 30, 2023
1 parent 90e9440 commit 12ccc15
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
package com.archimatetool.editor.model.commands;

import java.util.Objects;

import org.eclipse.gef.commands.Command;

import com.archimatetool.model.IFeatures;
Expand All @@ -24,8 +26,8 @@ public FeatureCommand(String label, IFeatures featuresObject, String name, Objec
setLabel(label);
features = featuresObject.getFeatures();
this.name = name;
this.value = value.toString();
this.defaultValue = defaultValue.toString();
this.value = value != null ? value.toString() : null;
this.defaultValue = defaultValue != null ? defaultValue.toString() : null;
oldValue = features.getString(name, this.defaultValue);
}

Expand All @@ -41,7 +43,7 @@ public void undo() {

@Override
public boolean canExecute() {
return value != null && !value.equals(oldValue);
return !Objects.equals(value, oldValue);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public interface IFeaturesEList extends EList<IFeature> {
/**
* Put or update a name/value as an IFeature
* @param name The name, non-null. If it already exists the value will be updated with the new value.
* @param value The value, non-null string.
* @param value The value, can be null
* @return the updated or new feature
*/
IFeature putString(String name, String value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
package com.archimatetool.model.impl;

import java.util.Objects;

import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.util.EObjectContainmentEList;

Expand All @@ -17,6 +19,7 @@
*
* @author Phillip Beauvoir
*/
@SuppressWarnings("nls")
public class FeaturesEList extends EObjectContainmentEList<IFeature> implements IFeaturesEList {

public FeaturesEList(Class<?> dataClass, InternalEObject owner, int featureID) {
Expand All @@ -31,10 +34,9 @@ public IFeature putString(String name, String value) {
@Override
public IFeature putString(String name, String value, String defaultValue) {
checkNull(name);
checkNull(value);

// value == default value so remove it or don't add it and return null
if(value.equals(defaultValue)) {
if(Objects.equals(value, defaultValue)) {
remove(name);
return null;
}
Expand All @@ -49,7 +51,7 @@ public IFeature putString(String name, String value, String defaultValue) {
add(feature);
}
// Different value
else if(!value.equals(feature.getValue())) {
else if(!Objects.equals(value, feature.getValue())) {
feature.setValue(value);
}

Expand Down Expand Up @@ -131,7 +133,7 @@ public IFeature getFeature(String name) {

private void checkNull(String s) {
if(s == null) {
throw new IllegalArgumentException("key or value cannot be null"); //$NON-NLS-1$
throw new IllegalArgumentException("name cannot be null");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,26 @@ public void putStringWithDefaultValueShouldNotAddFeature() {
assertTrue(list.isEmpty());
}

@Test
public void putStringNullWithDefault() {
IFeature feature = list.putString("name1", "value", "default");
assertNotNull(feature);

feature = list.putString("name1", null, "default");
assertEquals("name1", list.get(0).getName());
assertNull(list.get(0).getValue());
}

@Test
public void putStringNullWithNullDefault() {
IFeature feature = list.putString("name1", "value", null);
assertNotNull(feature);

feature = list.putString("name1", null, null);
assertNull(feature);
assertTrue(list.isEmpty());
}

@Test
public void putInt() {
IFeature feature = list.putInt("name1", 1);
Expand Down

0 comments on commit 12ccc15

Please sign in to comment.