001: /*
002: * ====================================================================
003: * Copyright (c) 2004-2008 TMate Software Ltd. All rights reserved.
004: *
005: * This software is licensed as described in the file COPYING, which
006: * you should have received as part of this distribution. The terms
007: * are also available at http://svnkit.com/license.html
008: * If newer versions of this license are posted there, you may use a
009: * newer version instead, at your option.
010: * ====================================================================
011: */
012: package org.tmatesoft.svn.core.internal.wc.admin;
013:
014: import java.util.Collection;
015: import java.util.HashMap;
016: import java.util.Iterator;
017: import java.util.Map;
018: import java.util.TreeSet;
019:
020: import org.tmatesoft.svn.core.SVNException;
021:
022: /**
023: * @version 1.1.1
024: * @author TMate Software Ltd.
025: */
026: public abstract class SVNVersionedProperties {
027: private Map myProperties;
028: private boolean myIsModified;
029:
030: protected SVNVersionedProperties(Map props) {
031: myProperties = props;
032: myIsModified = false;
033: }
034:
035: public abstract boolean containsProperty(String name)
036: throws SVNException;
037:
038: public abstract String getPropertyValue(String name)
039: throws SVNException;
040:
041: public boolean isModified() {
042: return myIsModified;
043: }
044:
045: protected void setModified(boolean modified) {
046: myIsModified = modified;
047: }
048:
049: public boolean isEmpty() throws SVNException {
050: Map props = loadProperties();
051: return props == null || props.isEmpty();
052: }
053:
054: public Collection getPropertyNames(Collection target)
055: throws SVNException {
056: Map props = loadProperties();
057:
058: target = target == null ? new TreeSet() : target;
059: if (isEmpty()) {
060: return target;
061: }
062: for (Iterator names = props.keySet().iterator(); names
063: .hasNext();) {
064: target.add(names.next());
065: }
066: return target;
067: }
068:
069: public void setPropertyValue(String name, String value)
070: throws SVNException {
071: Map props = loadProperties();
072: if (value != null) {
073: props.put(name, value);
074: } else {
075: props.remove(name);
076: }
077: myIsModified = true;
078: }
079:
080: public SVNVersionedProperties compareTo(
081: SVNVersionedProperties properties) throws SVNException {
082: Map result = new HashMap();
083: if (isEmpty()) {
084: result.putAll(properties.asMap());
085: return wrap(result);
086: }
087:
088: Collection props1 = getPropertyNames(null);
089: Collection props2 = properties.getPropertyNames(null);
090:
091: // missed in props2.
092: Collection tmp = new TreeSet(props1);
093: tmp.removeAll(props2);
094: for (Iterator props = tmp.iterator(); props.hasNext();) {
095: String missing = (String) props.next();
096: result.put(missing, null);
097: }
098:
099: // added in props2.
100: tmp = new TreeSet(props2);
101: tmp.removeAll(props1);
102:
103: for (Iterator props = tmp.iterator(); props.hasNext();) {
104: String added = (String) props.next();
105: result.put(added, properties.getPropertyValue(added));
106: }
107:
108: // changed in props2
109: props2.retainAll(props1);
110: for (Iterator props = props2.iterator(); props.hasNext();) {
111: String changed = (String) props.next();
112: String value1 = getPropertyValue(changed);
113: String value2 = properties.getPropertyValue(changed);
114: if (!value1.equals(value2)) {
115: result.put(changed, value2);
116: }
117: }
118: return wrap(result);
119: }
120:
121: public void copyTo(SVNVersionedProperties destination)
122: throws SVNException {
123: Map props = loadProperties();
124: if (isEmpty()) {
125: destination.removeAll();
126: } else {
127: destination.put(props);
128: }
129: }
130:
131: public void removeAll() throws SVNException {
132: Map props = loadProperties();
133: if (!isEmpty()) {
134: props.clear();
135: myIsModified = true;
136: }
137: }
138:
139: public boolean equals(SVNVersionedProperties props)
140: throws SVNException {
141: return compareTo(props).isEmpty();
142: }
143:
144: public Map asMap() throws SVNException {
145: Map props = loadProperties() != null ? new HashMap(
146: loadProperties()) : new HashMap();
147: return props;
148: }
149:
150: protected void put(Map props) throws SVNException {
151: Map this Props = loadProperties();
152: this Props.clear();
153: this Props.putAll(props);
154: myIsModified = true;
155: }
156:
157: protected Map getPropertiesMap() {
158: return myProperties;
159: }
160:
161: protected void setPropertiesMap(Map props) {
162: myProperties = props;
163: }
164:
165: protected abstract SVNVersionedProperties wrap(Map properties);
166:
167: protected abstract Map loadProperties() throws SVNException;
168:
169: }
|