Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright 2011 the original author or authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package org.springframework.data.neo4j.fieldaccess;
 
 import java.util.Map;
 import java.util.Set;

Stores the properties internally with prefixed keys. When using the methods from DynamicProperties the prefix is dynamically added and removed so that the prefixing is not visible when using the DynamicProperties interface.

The methods *PrefixedProperty() allow to access the prefixed property key/values pairs directly.

 
 public class PrefixedDynamicProperties implements DynamicProperties , Serializable {
 
     private static final long serialVersionUID = 1L;
 
     private Object writeReplace() {
         return new SerializationProxy(this);
     }
 
     private void readObject(ObjectInputStream oisthrows InvalidObjectException {
         throw new InvalidObjectException("Proxy required");
     }
 
     private transient final Map<StringObjectmap;
     protected final transient String prefix;
     protected transient boolean dirty;

    
Handles key prefixing
 
     private static class PrefixUtil {
         private final String prefix;
         private final static String DIVIDER = "-";
 
         public PrefixUtil(final String prefix) {
             this. = prefix + ;
         }
 
         boolean hasPrefix(final String s) {
             return s.startsWith();
         }
 
         public String removePrefix(final String s) {
             if (hasPrefix(s)) {
                 return s.substring(.length());
             }
             else {
                 return s;
             }
         }
 
         public static String prefixKey(final String prefixfinal String key) {
             return new StringBuilder(prefix).append().append(key).toString();
         }
     }

    
Removes a prefix from Strings when iterating over them.
 
     private static class RemovePrefixIterableWrapper implements Iterable<String> {
 
         private final Iterable<Stringiterable;
         private final String prefix;

        

Parameters:
iterable Strings to iterate over
prefix the prefix to have removed from the iterated Strings
 
         RemovePrefixIterableWrapper(final Iterable<Stringiterablefinal String prefix) {
             this. = iterable;
             this. = prefix;
         }
 
         @Override
         public Iterator<Stringiterator() {
            return new RemovePrefixIteratorWrapper(.iterator(), );
        }

        
Removes a prefix from Strings when iterating over them.
        private static class RemovePrefixIteratorWrapper implements Iterator<String> {
            private final Iterator<Stringit;
            private final PrefixUtil prefixUtil;

            

Parameters:
it Strings to iterate over
prefix the prefix to have removed from the iterated Strings
            private RemovePrefixIteratorWrapper(final Iterator<Stringitfinal String prefix) {
                this. = new PrefixUtil(prefix);
                this. = it;
            }
            @Override
            public boolean hasNext() {
                return .hasNext();
            }

            
Returns the next string with the prefix removed
            @Override
            public String next() {
                return .removePrefix(.next());
            }
            @Override
            public void remove() {
                .remove();
            }
        }
    }

    

Parameters:
prefix the prefix to be added internally to the keys
    public PrefixedDynamicProperties(final String prefix) {
         = new HashMap<StringObject>();
        this. = prefix;
    }

    

Parameters:
prefix the prefix to be added internally to the keys
initialCapacity the initialCapacity of the internal map that holds the properties
    public PrefixedDynamicProperties(final String prefixfinal int initialCapacity) {
         = new HashMap<StringObject>(initialCapacity);
        this. = prefix;
    }
    @Override
    public boolean hasProperty(final String key) {
        return .containsKey(prefixedKey(key));
    }
    @Override
    public Object getProperty(final String key) {
        return .get(prefixedKey(key));
    }
    @Override
    public Object getProperty(final String keyfinal Object defaultValue) {
        if (!hasProperty(key)) {
            return defaultValue;
        }
        return getProperty(key);
    }
    @Override
    public void setProperty(final String keyfinal Object value) {
        baseSetProperty(keyvalue);
    }
    private void baseSetProperty(final String keyfinal Object value) {
        .put(prefixedKey(key), value);
        setDirty(true);
    }
    private Object baseRemoveProperty(final String key) {
        Object result = .remove(prefixedKey(key));
        setDirty(true);
        return result;
    }
    
    @Override
    public Object removeProperty(final String key) {
        return baseRemoveProperty(key);
    }
    @Override
    public Iterable<StringgetPropertyKeys() {
        return new RemovePrefixIterableWrapper(.keySet(), );
    }
    @Override
    public void setPropertiesFrom(final Map<StringObjectpropertiesMap) {
        this..clear();
        for (String key : propertiesMap.keySet()) {
            baseSetProperty(keypropertiesMap.get(key));
        }
    }
    @Override
    public DynamicProperties createFrom(final Map<StringObjectmap) {
        DynamicProperties d = new PrefixedDynamicProperties(map.size());
        d.setPropertiesFrom(map);
        return d;
    }
    @Override
    public Map<StringObjectasMap() {
        Map<StringObjectresult = new HashMap<StringObject>(.size());
        for (String key : getPropertyKeys()) {
            result.put(keygetProperty(key));
        }
        return result;
    }

    
Set the property with the given key only if the key is prefixed.

Parameters:
key key of the property
value value
Returns:
true if the property has been set or not
    public boolean setPropertyIfPrefixed(final String keyfinal Object value) {
        if (isPrefixedKey(key)) {
            setPrefixedProperty(keyvalue);
            return true;
        }
        return false;
    }
    public boolean isPrefixedKey(String key) {
        PrefixUtil util = new PrefixUtil();
        return util.hasPrefix(key);
    }
    
    private String prefixedKey(final String key) {
        return PrefixUtil.prefixKey(key);
    }
    public Object getPrefixedProperty(final String key) {
        return .get(key);
    }
    public void setPrefixedProperty(final String keyfinal Object value) {
        .put(keyvalue);
        setDirty(true);
    }
    public boolean hasPrefixedProperty(final String key) {
        return .containsKey(key);
    }
    public Set<StringgetPrefixedPropertyKeys() {
        return .keySet();
    }
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (( == null) ? 0 : .hashCode());
		result = prime * result + (( == null) ? 0 : .hashCode());
		return result;
	}
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		if ( == null) {
			if (other.map != null) {
				return false;
			}
else if (!.equals(other.map)) {
			return false;
		}
		if ( == null) {
			if (other.prefix != null) {
				return false;
			}
else if (!.equals(other.prefix)) {
			return false;
		}
		return true;
	}

    
Implementation of the Serialization Proxy Pattern (ref Item 78 of Effective Java - 2nd edition)

Parameters:
<T> Type of the underlying class being stored in the Set.
    private static class SerializationProxy<T> implements Serializable {
        private static final long serialVersionUID = 1L;
        private Map actualMapContent;
        private String prefix;
        SerializationProxy(PrefixedDynamicProperties prefixedDynamicProperties) {
            this. = prefixedDynamicProperties.map;
            this. = prefixedDynamicProperties.prefix;
        }
        private Object readResolve() {
            PrefixedDynamicProperties val = new PrefixedDynamicProperties();
            val.setPropertiesFrom();
            return val;
        }
    }
    public boolean isDirty() {
        return ;
    }
    public void setDirty(boolean dirty) {
        this. = true;
    }
New to GrepCode? Check out our FAQ X