This project has retired. For details please refer to its Attic page.
PropertiesImpl xref

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.chemistry.opencmis.commons.impl.dataobjects;
20  
21  import java.util.ArrayList;
22  import java.util.Collection;
23  import java.util.Collections;
24  import java.util.Iterator;
25  import java.util.LinkedHashMap;
26  import java.util.List;
27  import java.util.Map;
28  
29  import org.apache.chemistry.opencmis.commons.data.Properties;
30  import org.apache.chemistry.opencmis.commons.data.PropertyData;
31  
32  /**
33   * Properties data implementation.
34   */
35  public class PropertiesImpl extends AbstractExtensionData implements Properties {
36  
37      private static final long serialVersionUID = 1L;
38  
39      List<PropertyData<?>> propertyList = new ArrayList<PropertyData<?>>();
40      Map<String, PropertyData<?>> properties = new LinkedHashMap<String, PropertyData<?>>();
41  
42      /**
43       * Constructor.
44       */
45      public PropertiesImpl() {
46      }
47  
48      /**
49       * Constructor.
50       * 
51       * @param properties
52       *            initial collection of properties
53       */
54      public PropertiesImpl(Collection<PropertyData<?>> properties) {
55          if (properties != null) {
56              for (PropertyData<?> prop : properties) {
57                  addProperty(prop);
58              }
59          }
60      }
61  
62      public Map<String, PropertyData<?>> getProperties() {
63          return Collections.unmodifiableMap(properties);
64      }
65  
66      public List<PropertyData<?>> getPropertyList() {
67          return Collections.unmodifiableList(propertyList);
68      }
69  
70      /**
71       * Adds a property.
72       * 
73       * @param property
74       *            the property
75       */
76      public void addProperty(PropertyData<?> property) {
77          if (property == null) {
78              return;
79          }
80  
81          propertyList.add(property);
82          properties.put(property.getId(), property);
83      }
84  
85      /**
86       * Replaces a property.
87       * 
88       * @param property
89       *            the property
90       */
91      public void replaceProperty(PropertyData<?> property) {
92          if ((property == null) || (property.getId() == null)) {
93              return;
94          }
95  
96          removeProperty(property.getId());
97  
98          propertyList.add(property);
99          properties.put(property.getId(), property);
100     }
101 
102     /**
103      * Removes a property.
104      * 
105      * @param id
106      *            the property id
107      */
108     public void removeProperty(String id) {
109         if (id == null) {
110             return;
111         }
112 
113         Iterator<PropertyData<?>> iterator = propertyList.iterator();
114         while (iterator.hasNext()) {
115             PropertyData<?> property = iterator.next();
116             if (id.equals(property.getId())) {
117                 iterator.remove();
118                 break;
119             }
120         }
121 
122         properties.remove(id);
123     }
124 
125     @Override
126     public String toString() {
127         return "Properties Data [properties=" + propertyList + "]" + super.toString();
128     }
129 
130 }