View | Details | Raw Unified | Return to bug 325131 | Differences between
and this patch

Collapse All | Expand All

(-)dom/org/eclipse/jdt/core/dom/rewrite/ASTRewrite.java (+133 lines)
Lines 10-15 Link Here
10
 *******************************************************************************/
10
 *******************************************************************************/
11
package org.eclipse.jdt.core.dom.rewrite;
11
package org.eclipse.jdt.core.dom.rewrite;
12
12
13
import java.util.HashMap;
13
import java.util.Iterator;
14
import java.util.Iterator;
14
import java.util.List;
15
import java.util.List;
15
import java.util.Map;
16
import java.util.Map;
Lines 103-108 Link Here
103
	 * @since 3.1
104
	 * @since 3.1
104
	 */
105
	 */
105
	private TargetSourceRangeComputer targetSourceRangeComputer = null;
106
	private TargetSourceRangeComputer targetSourceRangeComputer = null;
107
	
108
	/**
109
	 * Primary field used in representing rewrite properties efficiently.
110
	 * If <code>null</code>, this rewrite has no properties.
111
	 * If a <code>String</code>, this is the name of this rewrite's sole property,
112
	 * and <code>property2</code> contains its value.
113
	 * If a <code>HashMap</code>, this is the table of property name-value
114
	 * mappings; <code>property2</code>, if non-null is its unmodifiable
115
	 * equivalent.
116
	 * Initially <code>null</code>.
117
	 * 
118
	 * @see #property2
119
	 */
120
	private Object property1 = null;
121
122
	/**
123
	 * Auxillary field used in representing rewrite properties efficiently.
124
	 *
125
	 * @see #property1
126
	 */
127
	private Object property2 = null;
106
128
107
	/**
129
	/**
108
	 * Creates a new instance for describing manipulations of
130
	 * Creates a new instance for describing manipulations of
Lines 702-705 Link Here
702
		}
724
		}
703
		return buf.toString();
725
		return buf.toString();
704
	}
726
	}
727
	
728
	/**
729
	 * Returns the named property of this rewrite, or <code>null</code> if none.
730
	 *
731
	 * @param propertyName the property name
732
	 * @return the property value, or <code>null</code> if none
733
	 * @see #setProperty(String,Object)
734
	 * @since 3.7
735
	 */
736
	public final Object getProperty(String propertyName) {
737
		if (propertyName == null) {
738
			throw new IllegalArgumentException();
739
		}
740
		if (this.property1 == null) {
741
			// rewrite has no properties at all
742
			return null;
743
		}
744
		if (this.property1 instanceof String) {
745
			// rewrite has only a single property
746
			if (propertyName.equals(this.property1)) {
747
				return this.property2;
748
			} else {
749
				return null;
750
			}
751
		}
752
		// otherwise rewrite has table of properties
753
		Map m = (Map) this.property1;
754
		return m.get(propertyName);
755
	}
756
	
757
	/**
758
	 * Sets the named property of this rewrite to the given value,
759
	 * or to <code>null</code> to clear it.
760
	 * <p>
761
	 * Clients should employ property names that are sufficiently unique
762
	 * to avoid inadvertent conflicts with other clients that might also be
763
	 * setting properties on the same rewrite.
764
	 * </p>
765
	 * <p>
766
	 * Note that modifying a property is not considered a modification to the
767
	 * AST itself. This is to allow clients to decorate existing rewrites with
768
	 * their own properties without jeopardizing certain things (like the
769
	 * validity of bindings), which rely on the underlying tree remaining static.
770
	 * </p>
771
	 *
772
	 * @param propertyName the property name
773
	 * @param data the new property value, or <code>null</code> if none
774
	 * @see #getProperty(String)
775
	 * @since 3.7
776
	 */
777
	public final void setProperty(String propertyName, Object data) {
778
		if (propertyName == null) {
779
			throw new IllegalArgumentException();
780
		}
781
782
		if (this.property1 == null) {
783
			// rewrite has no properties at all
784
			if (data == null) {
785
				// rewrite already knows this
786
				return;
787
			}
788
			// rewrite gets its fist property
789
			this.property1 = propertyName;
790
			this.property2 = data;
791
			return;
792
		}
793
794
		if (this.property1 instanceof String) {
795
			// rewrite has only a single property
796
			if (propertyName.equals(this.property1)) {
797
				// we're in luck
798
				this.property2 = data;
799
				if (data == null) {
800
					// just delete last property
801
					this.property1 = null;
802
					this.property2 = null;
803
				}
804
				return;
805
			}
806
			if (data == null) {
807
				// we already know this
808
				return;
809
			}
810
			// rewrite already has one property - getting its second
811
			// convert to more flexible representation
812
			HashMap m = new HashMap(2);
813
			m.put(this.property1, this.property2);
814
			m.put(propertyName, data);
815
			this.property1 = m;
816
			this.property2 = null;
817
			return;
818
		}
819
820
		// rewrite has two or more properties
821
		HashMap m = (HashMap) this.property1;
822
		if (data == null) {
823
			m.remove(propertyName);
824
			// check for just one property left
825
			if (m.size() == 1) {
826
				// convert to more efficient representation
827
				Map.Entry[] entries = (Map.Entry[]) m.entrySet().toArray(new Map.Entry[1]);
828
				this.property1 = entries[0].getKey();
829
				this.property2 = entries[0].getValue();
830
			}
831
			return;
832
		} else {
833
			m.put(propertyName, data);
834
			// still has two or more properties
835
			return;
836
		}
837
	}
705
}
838
}

Return to bug 325131