View Javadoc

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.client.runtime;
20  
21  import java.io.Serializable;
22  import java.util.ArrayList;
23  import java.util.GregorianCalendar;
24  import java.util.List;
25  
26  import org.apache.chemistry.opencmis.client.api.Property;
27  import org.apache.chemistry.opencmis.commons.definitions.PropertyDefinition;
28  import org.apache.chemistry.opencmis.commons.enums.Cardinality;
29  import org.apache.chemistry.opencmis.commons.enums.PropertyType;
30  import org.apache.chemistry.opencmis.commons.impl.dataobjects.AbstractPropertyData;
31  
32  /**
33   * Property Implementation.
34   */
35  public class PropertyImpl<T> extends AbstractPropertyData<T> implements Property<T>, Serializable {
36  
37      private static final long serialVersionUID = 1L;
38      private final PropertyDefinition<T> propertyDefinition;
39  
40      /**
41       * Constructs a property from a list of values.
42       */
43      public PropertyImpl(PropertyDefinition<T> pd, List<T> values) {
44          if (pd == null) {
45              throw new IllegalArgumentException("Type must be set!");
46          }
47          if (values == null) {
48              throw new IllegalArgumentException("Value must be set!");
49          }
50          propertyDefinition = pd;
51          initialize(pd);
52          setValues(values);
53      }
54  
55      /**
56       * Copy constructor.
57       */
58      public PropertyImpl(Property<T> property) {
59          if (property == null) {
60              throw new IllegalArgumentException("Source must be set!");
61          }
62  
63          propertyDefinition = property.getDefinition();
64          initialize(property.getDefinition());
65          setValues(new ArrayList<T>(property.getValues()));
66      }
67  
68      protected void initialize(PropertyDefinition<?> pd) {
69          setId(pd.getId());
70          setDisplayName(pd.getDisplayName());
71          setLocalName(pd.getLocalName());
72          setQueryName(pd.getQueryName());
73      }
74  
75      @Override
76      public PropertyDefinition<T> getDefinition() {
77          return propertyDefinition;
78      }
79  
80      @Override
81      public PropertyType getType() {
82          return propertyDefinition.getPropertyType();
83      }
84  
85      @Override
86      @SuppressWarnings("unchecked")
87      public <U> U getValue() {
88          List<T> values = getValues();
89          if (propertyDefinition.getCardinality() == Cardinality.SINGLE) {
90              return values.isEmpty() ? null : (U) values.get(0);
91          } else {
92              return (U) values;
93          }
94      }
95  
96      @Override
97      public String getValueAsString() {
98          List<T> values = getValues();
99          if (values.isEmpty()) {
100             return null;
101         }
102 
103         return formatValue(values.get(0));
104     }
105 
106     @Override
107     public String getValuesAsString() {
108         List<T> values = getValues();
109 
110         StringBuilder result = new StringBuilder(128);
111         for (T value : values) {
112             if (result.length() > 0) {
113                 result.append(", ");
114             }
115 
116             result.append(formatValue(value));
117         }
118 
119         return "[" + result.toString() + "]";
120     }
121 
122     private String formatValue(T value) {
123         String result;
124 
125         if (value == null) {
126             return null;
127         }
128 
129         if (value instanceof GregorianCalendar) {
130             result = ((GregorianCalendar) value).getTime().toString();
131         } else {
132             result = value.toString();
133         }
134 
135         return result;
136     }
137 
138     @Override
139     public boolean isMultiValued() {
140         return propertyDefinition.getCardinality() == Cardinality.MULTI;
141     }
142 }