diff options
author | bvosburgh | 2010-03-18 15:56:51 +0000 |
---|---|---|
committer | bvosburgh | 2010-03-18 15:56:51 +0000 |
commit | 7eedcde8161c8b43bfaa4604580175e013ff9091 (patch) | |
tree | 5347f16cc4a9e94349ede0ac070030e0653fb103 /jpa/plugins/org.eclipse.jpt.utility | |
parent | 73a21c510df1670c96895e613a44cf2f3c2617ac (diff) | |
download | webtools.dali-7eedcde8161c8b43bfaa4604580175e013ff9091.tar.gz webtools.dali-7eedcde8161c8b43bfaa4604580175e013ff9091.tar.xz webtools.dali-7eedcde8161c8b43bfaa4604580175e013ff9091.zip |
fix text field flakiness; fix some widget enablement behavior
Diffstat (limited to 'jpa/plugins/org.eclipse.jpt.utility')
10 files changed, 510 insertions, 59 deletions
diff --git a/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/ArrayTools.java b/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/ArrayTools.java index 1f17389033..f1d37d51b6 100644 --- a/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/ArrayTools.java +++ b/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/ArrayTools.java @@ -29,6 +29,16 @@ public final class ArrayTools { // ********** instantiation ********** /** + * Return a new array with the same length + * and the same component type as the specified array. + * <p> + * <code>Arrays.newArray(Object[] array)</code> + */ + public static <E> E[] newArray(E[] array) { + return newArray(array, array.length); + } + + /** * Return a new array with the specified length * and the same component type as the specified array. * <p> diff --git a/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/BooleanTransformer.java b/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/BooleanTransformer.java new file mode 100644 index 0000000000..31354c69e3 --- /dev/null +++ b/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/BooleanTransformer.java @@ -0,0 +1,78 @@ +/******************************************************************************* + * Copyright (c) 2010 Oracle. 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. + * + * Contributors: + * Oracle - initial API and implementation + ******************************************************************************/ +package org.eclipse.jpt.utility.internal; + +/** + * A <code>BooleanTransformer</code> will transform a possibly-null + * {@link Boolean} to a non-null {@link Boolean}:<ul> + * <li>When the original {@link Boolean} is <em>not</em> <code>null</code>, + * the transformer will return it unchanged. + * <li>When the original {@link Boolean} is <code>null</code>, + * the transformer will return its default value ({@link Boolean#TRUE} + * or {@link Boolean#FALSE}). + * </ul> + */ +public final class BooleanTransformer + implements Transformer<Boolean, Boolean> +{ + // not null + private final Boolean nullValue; + + /** + * A {@link Transformer} that will return the original {@link Boolean} when + * it is non-<code>null</code>; otherwise the {@link Transformer} will return + * {@link Boolean#TRUE}. + */ + public static final Transformer<Boolean, Boolean> TRUE = new BooleanTransformer(Boolean.TRUE); + + /** + * A {@link Transformer} that will return the original {@link Boolean} when + * it is non-<code>null</code>; otherwise the {@link Transformer} will return + * {@link Boolean#FALSE}. + */ + public static final Transformer<Boolean, Boolean> FALSE = new BooleanTransformer(Boolean.FALSE); + + /** + * Return a transformer that will return the specified value if the original + * value is <code>null</code>. + */ + public Transformer<Boolean, Boolean> valueOf(Boolean b) { + return valueOf(b.booleanValue()); + } + + /** + * Return a transformer that will return the specified value if the original + * value is <code>null</code>. + */ + public Transformer<Boolean, Boolean> valueOf(boolean b) { + return b ? TRUE : FALSE; + } + + /** + * Ensure only 2 constant versions. + */ + private BooleanTransformer(Boolean nullValue) { + super(); + if (nullValue == null) { + throw new NullPointerException(); + } + this.nullValue = nullValue; + } + + public Boolean transform(Boolean b) { + return (b != null) ? b : this.nullValue; + } + + @Override + public String toString() { + return StringTools.buildToStringFor(this, this.nullValue); + } + +} diff --git a/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/Tools.java b/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/Tools.java index 2b4eb3fe70..39378dcab5 100644 --- a/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/Tools.java +++ b/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/Tools.java @@ -13,7 +13,7 @@ package org.eclipse.jpt.utility.internal; * Various utility methods. */ @SuppressWarnings("nls") -public class Tools { +public final class Tools { // ********** object comparison ********** @@ -21,7 +21,9 @@ public class Tools { * Return whether the specified values are equal, with the appropriate null checks. */ public static boolean valuesAreEqual(Object value1, Object value2) { - return (value1 == null) ? (value2 == null) : value1.equals(value2); + return (value1 == null) ? + (value2 == null) : + ((value2 != null) && value1.equals(value2)); } /** diff --git a/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/Transformer.java b/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/Transformer.java index 79c2bab8e7..a77ef1155a 100644 --- a/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/Transformer.java +++ b/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/Transformer.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2005, 2007 Oracle. All rights reserved. + * Copyright (c) 2005, 2010 Oracle. 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. @@ -25,6 +25,10 @@ public interface Transformer<T1, T2> { T2 transform(T1 o); + /** + * A "null" transformer will perform no transformation at all; + * it will simply return the object "untransformed". + */ final class Null<S1, S2> implements Transformer<S1, S2>, Serializable { @SuppressWarnings("unchecked") public static final Transformer INSTANCE = new Null(); @@ -52,6 +56,12 @@ public interface Transformer<T1, T2> { } } + /** + * A "disabled" transformer will throw an exception if + * {@link #transform(Object)} is called. This is useful in situations + * where a transformer is optional and the default transformer should + * not be used. + */ final class Disabled<S1, S2> implements Transformer<S1, S2>, Serializable { @SuppressWarnings("unchecked") public static final Transformer INSTANCE = new Disabled(); diff --git a/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/model/value/AbstractPropertyValueModelAdapter.java b/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/model/value/AbstractPropertyValueModelAdapter.java index 150ed11245..641456d90d 100644 --- a/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/model/value/AbstractPropertyValueModelAdapter.java +++ b/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/model/value/AbstractPropertyValueModelAdapter.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2008, 2009 Oracle. All rights reserved. + * Copyright (c) 2008, 2010 Oracle. 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. @@ -69,8 +69,8 @@ public abstract class AbstractPropertyValueModelAdapter<V> @Override protected void engageModel() { this.engageModel_(); - // synch our value *after* we start listening to the collection, - // since the collection's value might change when a listener is added + // synch our value *after* we start listening to the model, + // since the model's value might change when a listener is added this.value = this.buildValue(); } @@ -91,7 +91,7 @@ public abstract class AbstractPropertyValueModelAdapter<V> @Override protected void disengageModel() { this.disengageModel_(); - // clear out our value when we are not listening to the collection + // clear out our value when we are not listening to the model this.value = null; } @@ -106,8 +106,7 @@ public abstract class AbstractPropertyValueModelAdapter<V> */ protected void propertyChanged() { Object old = this.value; - this.value = this.buildValue(); - this.firePropertyChanged(VALUE, old, this.value); + this.firePropertyChanged(VALUE, old, this.value = this.buildValue()); } @Override diff --git a/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/model/value/CollectionPropertyValueModelAdapter.java b/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/model/value/CollectionPropertyValueModelAdapter.java index be5a7c2bb4..d63ed69a57 100644 --- a/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/model/value/CollectionPropertyValueModelAdapter.java +++ b/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/model/value/CollectionPropertyValueModelAdapter.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2009 Oracle. All rights reserved. + * Copyright (c) 2007, 2010 Oracle. 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. @@ -39,7 +39,7 @@ public abstract class CollectionPropertyValueModelAdapter<V> extends AbstractPropertyValueModelAdapter<V> { /** The wrapped collection value model. */ - protected final CollectionValueModel<?> collectionHolder; + protected final CollectionValueModel<?> collectionModel; /** A listener that allows us to synch with changes to the wrapped collection holder. */ protected final CollectionChangeListener collectionChangeListener; @@ -51,9 +51,9 @@ public abstract class CollectionPropertyValueModelAdapter<V> * Construct a property value model with the specified wrapped * collection value model. */ - protected CollectionPropertyValueModelAdapter(CollectionValueModel<?> collectionHolder) { + protected CollectionPropertyValueModelAdapter(CollectionValueModel<?> collectionModel) { super(); - this.collectionHolder = collectionHolder; + this.collectionModel = collectionModel; this.collectionChangeListener = this.buildCollectionChangeListener(); } @@ -86,7 +86,7 @@ public abstract class CollectionPropertyValueModelAdapter<V> */ @Override protected void engageModel_() { - this.collectionHolder.addCollectionChangeListener(CollectionValueModel.VALUES, this.collectionChangeListener); + this.collectionModel.addCollectionChangeListener(CollectionValueModel.VALUES, this.collectionChangeListener); } /** @@ -94,7 +94,7 @@ public abstract class CollectionPropertyValueModelAdapter<V> */ @Override protected void disengageModel_() { - this.collectionHolder.removeCollectionChangeListener(CollectionValueModel.VALUES, this.collectionChangeListener); + this.collectionModel.removeCollectionChangeListener(CollectionValueModel.VALUES, this.collectionChangeListener); } diff --git a/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/model/value/CompositeBooleanPropertyValueModel.java b/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/model/value/CompositeBooleanPropertyValueModel.java new file mode 100644 index 0000000000..1bf5d25a46 --- /dev/null +++ b/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/model/value/CompositeBooleanPropertyValueModel.java @@ -0,0 +1,338 @@ +/******************************************************************************* + * Copyright (c) 2010 Oracle. 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. + * + * Contributors: + * Oracle - initial API and implementation + ******************************************************************************/ +package org.eclipse.jpt.utility.internal.model.value; + +import java.util.Collection; + +import org.eclipse.jpt.utility.internal.iterables.TransformationIterable; +import org.eclipse.jpt.utility.model.value.CollectionValueModel; +import org.eclipse.jpt.utility.model.value.PropertyValueModel; + +/** + * A <code>CompositeBooleanPropertyValueModel</code> adapts a + * {@link CollectionValueModel} holding boolean {@link PropertyValueModel}s + * to a single boolean {@link PropertyValueModel}. The composite value is + * determined by the {@link CompositeBooleanPropertyValueModel.Adapter} provided + * at construction. Typically the composite will be either an AND or an OR of + * all the boolean values. + * <p> + * If there are <em>no</em> boolean {@link PropertyValueModel}s, the composite + * will return its default value; which, by default, is <code>null</code>. + * <p> + * <strong>NB:</strong> None of the wrapped boolean models can return a + * <code>null</code> value or this class will throw an exception. The assumption + * is that all the wrapped boolean models are configured with "default" values + * that determine the model's value (TRUE or FALSE) when <code>null</code>. + */ +public class CompositeBooleanPropertyValueModel + extends CompositePropertyValueModel<Boolean> +{ + /** + * Calculation of the model's value is delegated to this adapter. + */ + protected final Adapter adapter; + + /** + * The default setting for the composite boolean property value model; + * for when the underlying collection model is empty. + * The default [default value] is <code>null</code>. + */ + private final Boolean defaultValue; + + + // ********** AND factory methods ********** + + /** + * Construct a boolean property value model that is a composite AND of the + * specified boolean property value models; i.e. the model's value is true + * if all the contained models are true, otherwise its value is false. + * The model's default value, when it contains no nested models, is + * <code>null</code>. + */ + public static CompositeBooleanPropertyValueModel and(PropertyValueModel<Boolean>... array) { + return new CompositeBooleanPropertyValueModel(AND_ADAPTER, array); + } + + /** + * Construct a boolean property value model that is a composite AND of the + * specified boolean property value models; i.e. the model's value is true + * if all the contained models are true, otherwise its value is false. + * When the model contains no nested models, its value will be the + * specified default. + */ + public static CompositeBooleanPropertyValueModel and(Boolean defaultValue, PropertyValueModel<Boolean>... array) { + return new CompositeBooleanPropertyValueModel(AND_ADAPTER, defaultValue, array); + } + + /** + * Construct a boolean property value model that is a composite AND of the + * specified boolean property value models; i.e. the model's value is true + * if all the contained models are true, otherwise its value is false. + * The model's default value, when it contains no nested models, is + * <code>null</code>. + */ + public static <E extends PropertyValueModel<Boolean>> CompositeBooleanPropertyValueModel and(Collection<E> collection) { + return new CompositeBooleanPropertyValueModel(AND_ADAPTER, collection); + } + + /** + * Construct a boolean property value model that is a composite AND of the + * specified boolean property value models; i.e. the model's value is true + * if all the contained models are true, otherwise its value is false. + * When the model contains no nested models, its value will be the + * specified default. + */ + public static <E extends PropertyValueModel<Boolean>> CompositeBooleanPropertyValueModel and(Boolean defaultValue, Collection<E> collection) { + return new CompositeBooleanPropertyValueModel(AND_ADAPTER, defaultValue, collection); + } + + /** + * Construct a boolean property value model that is a composite AND of the + * specified boolean property value models; i.e. the model's value is true + * if all the contained models are true, otherwise its value is false. + * The model's default value, when it contains no nested models, is + * <code>null</code>. + */ + public static CompositeBooleanPropertyValueModel and(CollectionValueModel<? extends PropertyValueModel<Boolean>> collectionModel) { + return new CompositeBooleanPropertyValueModel(AND_ADAPTER, collectionModel); + } + + /** + * Construct a boolean property value model that is a composite AND of the + * specified boolean property value models; i.e. the model's value is true + * if all the contained models are true, otherwise its value is false. + * When the model contains no nested models, its value will be the + * specified default. + */ + public static CompositeBooleanPropertyValueModel and(Boolean defaultValue, CollectionValueModel<? extends PropertyValueModel<Boolean>> collectionModel) { + return new CompositeBooleanPropertyValueModel(AND_ADAPTER, defaultValue, collectionModel); + } + + + // ********** OR factory methods ********** + + /** + * Construct a boolean property value model that is a composite OR of the + * specified boolean property value models; i.e. the model's value is false + * if all the contained models are false, otherwise its value is true. + * The model's default value, when it contains no nested models, is + * <code>null</code>. + */ + public static CompositeBooleanPropertyValueModel or(PropertyValueModel<Boolean>... array) { + return new CompositeBooleanPropertyValueModel(OR_ADAPTER, array); + } + + /** + * Construct a boolean property value model that is a composite OR of the + * specified boolean property value models; i.e. the model's value is false + * if all the contained models are false, otherwise its value is true. + * When the model contains no nested models, its value will be the + * specified default. + */ + public static CompositeBooleanPropertyValueModel or(Boolean defaultValue, PropertyValueModel<Boolean>... array) { + return new CompositeBooleanPropertyValueModel(OR_ADAPTER, defaultValue, array); + } + + /** + * Construct a boolean property value model that is a composite OR of the + * specified boolean property value models; i.e. the model's value is false + * if all the contained models are false, otherwise its value is true. + * The model's default value, when it contains no nested models, is + * <code>null</code>. + */ + public static <E extends PropertyValueModel<Boolean>> CompositeBooleanPropertyValueModel or(Collection<E> collection) { + return new CompositeBooleanPropertyValueModel(OR_ADAPTER, collection); + } + + /** + * Construct a boolean property value model that is a composite OR of the + * specified boolean property value models; i.e. the model's value is false + * if all the contained models are false, otherwise its value is true. + * When the model contains no nested models, its value will be the + * specified default. + */ + public static <E extends PropertyValueModel<Boolean>> CompositeBooleanPropertyValueModel or(Boolean defaultValue, Collection<E> collection) { + return new CompositeBooleanPropertyValueModel(OR_ADAPTER, defaultValue, collection); + } + + /** + * Construct a boolean property value model that is a composite OR of the + * specified boolean property value models; i.e. the model's value is false + * if all the contained models are false, otherwise its value is true. + * The model's default value, when it contains no nested models, is + * <code>null</code>. + */ + public static CompositeBooleanPropertyValueModel or(CollectionValueModel<? extends PropertyValueModel<Boolean>> collectionModel) { + return new CompositeBooleanPropertyValueModel(OR_ADAPTER, collectionModel); + } + + /** + * Construct a boolean property value model that is a composite OR of the + * specified boolean property value models; i.e. the model's value is false + * if all the contained models are false, otherwise its value is true. + * When the model contains no nested models, its value will be the + * specified default. + */ + public static CompositeBooleanPropertyValueModel or(Boolean defaultValue, CollectionValueModel<? extends PropertyValueModel<Boolean>> collectionModel) { + return new CompositeBooleanPropertyValueModel(OR_ADAPTER, defaultValue, collectionModel); + } + + + // ********** constructors ********** + + /** + * Construct a boolean property value model that is a composite of the specified + * boolean property value models, as defined by the specified adapter. + * The model's default value, when it contains no nested models, is + * <code>null</code>. + */ + public CompositeBooleanPropertyValueModel(Adapter adapter, PropertyValueModel<Boolean>... array) { + this(adapter, null, array); + } + + /** + * Construct a boolean property value model that is a composite of the specified + * boolean property value models, as defined by the specified adapter. + * When the model contains no nested models, its value will be the + * specified default. + */ + public CompositeBooleanPropertyValueModel(Adapter adapter, Boolean defaultValue, PropertyValueModel<Boolean>... array) { + super(array); + this.adapter = adapter; + this.defaultValue = defaultValue; + } + + /** + * Construct a boolean property value model that is a composite of the specified + * boolean property value models, as defined by the specified adapter. + * The model's default value, when it contains no nested models, is + * <code>null</code>. + */ + public <E extends PropertyValueModel<Boolean>> CompositeBooleanPropertyValueModel(Adapter adapter, Collection<E> collection) { + this(adapter, null, collection); + } + + /** + * Construct a boolean property value model that is a composite of the specified + * boolean property value models, as defined by the specified adapter. + * When the model contains no nested models, its value will be the + * specified default. + */ + public <E extends PropertyValueModel<Boolean>> CompositeBooleanPropertyValueModel(Adapter adapter, Boolean defaultValue, Collection<E> collection) { + super(collection); + this.adapter = adapter; + this.defaultValue = defaultValue; + } + + /** + * Construct a boolean property value model that is a composite of the specified + * boolean property value models, as defined by the specified adapter. + * The model's default value, when it contains no nested models, is + * <code>null</code>. + */ + public CompositeBooleanPropertyValueModel(Adapter adapter, CollectionValueModel<? extends PropertyValueModel<Boolean>> collectionModel) { + this(adapter, null, collectionModel); + } + + /** + * Construct a boolean property value model that is a composite of the specified + * boolean property value models, as defined by the specified adapter. + * When the model contains no nested models, its value will be the + * specified default. + */ + public CompositeBooleanPropertyValueModel(Adapter adapter, Boolean defaultValue, CollectionValueModel<? extends PropertyValueModel<Boolean>> collectionModel) { + super(collectionModel); + this.adapter = adapter; + this.defaultValue = defaultValue; + } + + + // ********** implementation ********** + + /** + * Return true if all the contained booleans are true; otherwise return + * false. + */ + @Override + protected Boolean buildValue() { + return (this.collectionModel.size() == 0) ? this.defaultValue : this.buildValue_(); + } + + protected Boolean buildValue_() { + return this.adapter.buildValue(this.getBooleans()); + } + + protected Iterable<Boolean> getBooleans() { + return new TransformationIterable<PropertyValueModel<Boolean>, Boolean>(this.getCollectionModel()) { + @Override + protected Boolean transform(PropertyValueModel<Boolean> booleanModel) { + return booleanModel.getValue(); + } + }; + } + + @Override + @SuppressWarnings("unchecked") + protected CollectionValueModel<? extends PropertyValueModel<Boolean>> getCollectionModel() { + return (CollectionValueModel<? extends PropertyValueModel<Boolean>>) super.getCollectionModel(); + } + + + // ********** adapter ********** + + /** + * This adapter allows the {@link CompositeBooleanPropertyValueModel} to be + * pluggable. + */ + public interface Adapter { + /** + * Return the composite boolean value of the specified collection + * of boolean models. + */ + Boolean buildValue(Iterable<Boolean> booleans); + } + + /** + * Return true if all the booleans are true; otherwise return false. + */ + public static final Adapter AND_ADAPTER = new Adapter() { + public Boolean buildValue(Iterable<Boolean> booleans) { + for (Boolean b : booleans) { + if ( ! b.booleanValue()) { + return Boolean.FALSE; + } + } + return Boolean.TRUE; + } + @Override + public String toString() { + return "AND_ADAPTER"; //$NON-NLS-1$ + } + }; + + /** + * Return false if all the booleans are false; otherwise return true. + */ + public static final Adapter OR_ADAPTER = new Adapter() { + public Boolean buildValue(Iterable<Boolean> booleans) { + for (Boolean b : booleans) { + if (b.booleanValue()) { + return Boolean.TRUE; + } + } + return Boolean.FALSE; + } + @Override + public String toString() { + return "OR_ADAPTER"; //$NON-NLS-1$ + } + }; + +} diff --git a/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/model/value/CompositePropertyValueModel.java b/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/model/value/CompositePropertyValueModel.java index a9cc888ddc..149500cc1d 100644 --- a/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/model/value/CompositePropertyValueModel.java +++ b/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/model/value/CompositePropertyValueModel.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009 Oracle. All rights reserved. + * Copyright (c) 2009, 2010 Oracle. 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. @@ -35,7 +35,6 @@ import org.eclipse.jpt.utility.model.value.PropertyValueModel; * </ul> * <strong>NB:</strong> The wrapped collection must not contain any duplicates * or this class will throw an exception. - * <p> */ public abstract class CompositePropertyValueModel<V> extends CollectionPropertyValueModelAdapter<V> @@ -58,7 +57,7 @@ public abstract class CompositePropertyValueModel<V> /** * Construct a property value model that is a composite of the specified - * property valuve models. + * property value models. */ public CompositePropertyValueModel(PropertyValueModel<?>... collection) { this(Arrays.asList(collection)); @@ -66,7 +65,7 @@ public abstract class CompositePropertyValueModel<V> /** * Construct a property value model that is a composite of the specified - * property valuve models. + * property value models. */ public <E extends PropertyValueModel<?>> CompositePropertyValueModel(Collection<E> collection) { this(new StaticCollectionValueModel<E>(collection)); @@ -74,10 +73,10 @@ public abstract class CompositePropertyValueModel<V> /** * Construct a property value model that is a composite of the specified - * property valuve models. + * property value models. */ - public CompositePropertyValueModel(CollectionValueModel<? extends PropertyValueModel<?>> collectionHolder) { - super(collectionHolder); + public CompositePropertyValueModel(CollectionValueModel<? extends PropertyValueModel<?>> collectionModel) { + super(collectionModel); this.propertyChangeListener = this.buildPropertyChangeListener(); } @@ -114,7 +113,7 @@ public abstract class CompositePropertyValueModel<V> @Override protected void engageModel_() { super.engageModel_(); - this.addComponentPVMs(this.getCollectionHolder()); + this.addComponentPVMs(this.getCollectionModel()); } protected <E extends PropertyValueModel<?>> void addComponentPVMs(Iterable<E> pvms) { @@ -126,7 +125,7 @@ public abstract class CompositePropertyValueModel<V> @Override protected void disengageModel_() { - this.removeComponentPVMs(this.getCollectionHolder()); + this.removeComponentPVMs(this.getCollectionModel()); super.disengageModel_(); } @@ -164,7 +163,7 @@ public abstract class CompositePropertyValueModel<V> @Override protected void collectionChanged(CollectionChangeEvent event) { this.removeAllComponentPVMs(); - this.addComponentPVMs(this.getCollectionHolder()); + this.addComponentPVMs(this.getCollectionModel()); super.collectionChanged(event); } @@ -176,8 +175,8 @@ public abstract class CompositePropertyValueModel<V> */ // minimize scope of suppressed warnings @SuppressWarnings("unchecked") - protected CollectionValueModel<? extends PropertyValueModel<?>> getCollectionHolder() { - return (CollectionValueModel<? extends PropertyValueModel<?>>) this.collectionHolder; + protected CollectionValueModel<? extends PropertyValueModel<?>> getCollectionModel() { + return (CollectionValueModel<? extends PropertyValueModel<?>>) this.collectionModel; } /** diff --git a/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/model/value/StaticCollectionValueModel.java b/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/model/value/StaticCollectionValueModel.java index d6abbba0b6..7d6d063d75 100644 --- a/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/model/value/StaticCollectionValueModel.java +++ b/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/model/value/StaticCollectionValueModel.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2009 Oracle. All rights reserved. + * Copyright (c) 2007, 2010 Oracle. 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. @@ -9,10 +9,11 @@ ******************************************************************************/ package org.eclipse.jpt.utility.internal.model.value; -import java.util.ArrayList; import java.util.Arrays; -import java.util.Collection; import java.util.Iterator; + +import org.eclipse.jpt.utility.internal.ArrayTools; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; import org.eclipse.jpt.utility.internal.model.AbstractModel; import org.eclipse.jpt.utility.model.value.CollectionValueModel; @@ -25,8 +26,8 @@ public class StaticCollectionValueModel<E> extends AbstractModel implements CollectionValueModel<E> { - /** The collection. */ - protected final Collection<E> collection; + /** The elements. */ + protected final Object[] elements; private static final long serialVersionUID = 1L; @@ -34,27 +35,31 @@ public class StaticCollectionValueModel<E> /** * Construct a static collection value model for the specified array. */ - public StaticCollectionValueModel(E... array) { - this(Arrays.asList(array)); + public StaticCollectionValueModel(E... elements) { + super(); + this.elements = elements.clone(); } /** - * Construct a static collection value model for the specified collection. + * Construct a static collection value model for the specified elements. */ - public StaticCollectionValueModel(Collection<? extends E> collection) { + public StaticCollectionValueModel(Iterable<? extends E> elements) { super(); - this.collection = new ArrayList<E>(collection); + this.elements = ArrayTools.array(elements); } // ********** CollectionValueModel implementation ********** public int size() { - return this.collection.size(); + return this.elements.length; } + @SuppressWarnings("unchecked") public Iterator<E> iterator() { - return this.collection.iterator(); + // we can cast here since our constructors require the elements to be + // of type E and ArrayIterator is read-only + return (Iterator<E>) new ArrayIterator<Object>(this.elements); } @@ -62,7 +67,7 @@ public class StaticCollectionValueModel<E> @Override public void toString(StringBuilder sb) { - sb.append(this.collection); + sb.append(Arrays.toString(this.elements)); } } diff --git a/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/model/value/StaticListValueModel.java b/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/model/value/StaticListValueModel.java index e672e7fdf9..c57c531b8b 100644 --- a/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/model/value/StaticListValueModel.java +++ b/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/model/value/StaticListValueModel.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2009 Oracle. All rights reserved. + * Copyright (c) 2007, 2010 Oracle. 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. @@ -9,13 +9,13 @@ ******************************************************************************/ package org.eclipse.jpt.utility.internal.model.value; -import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; -import java.util.List; import java.util.ListIterator; -import org.eclipse.jpt.utility.internal.iterators.ReadOnlyIterator; -import org.eclipse.jpt.utility.internal.iterators.ReadOnlyListIterator; + +import org.eclipse.jpt.utility.internal.ArrayTools; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; +import org.eclipse.jpt.utility.internal.iterators.ArrayListIterator; import org.eclipse.jpt.utility.internal.model.AbstractModel; import org.eclipse.jpt.utility.model.value.ListValueModel; @@ -28,48 +28,58 @@ public class StaticListValueModel<E> extends AbstractModel implements ListValueModel<E> { - /** The value. */ - protected final List<E> list; + /** The elements. */ + protected final Object[] elements; private static final long serialVersionUID = 1L; /** - * Construct a static list value model for the specified array. + * Construct a static list value model for the specified elements. */ - public StaticListValueModel(E... array) { - this(Arrays.asList(array)); + public StaticListValueModel(E... elements) { + super(); + this.elements = elements.clone(); } /** - * Construct a static list value model for the specified list. + * Construct a static list value model for the specified elements. */ - public StaticListValueModel(List<? extends E> list) { + public StaticListValueModel(Iterable<? extends E> elements) { super(); - this.list = new ArrayList<E>(list); + this.elements = ArrayTools.array(elements); } // ********** ListValueModel implementation ********** + @SuppressWarnings("unchecked") public Iterator<E> iterator() { - return new ReadOnlyIterator<E>(this.list.iterator()); + // we can cast here since our constructors require the elements to be + // of type E and ArrayIterator is read-only + return (Iterator<E>) new ArrayIterator<Object>(this.elements); } + @SuppressWarnings("unchecked") public ListIterator<E> listIterator() { - return new ReadOnlyListIterator<E>(this.list.listIterator()); + // we can cast here since our constructors require the elements to be + // of type E and ArrayListIterator is read-only + return (ListIterator<E>) new ArrayListIterator<Object>(this.elements); } public int size() { - return this.list.size(); + return this.elements.length; } + @SuppressWarnings("unchecked") public E get(int index) { - return this.list.get(index); + // we can cast here since our constructors require the elements to be + // of type E + return (E) this.elements[index]; } public Object[] toArray() { - return this.list.toArray(); + return this.elements.clone(); } @@ -77,7 +87,7 @@ public class StaticListValueModel<E> @Override public void toString(StringBuilder sb) { - sb.append(this.list); + sb.append(Arrays.toString(this.elements)); } } |