This project has retired. For details please refer to its Attic page.
PropertyImpl 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.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      protected void initialize(PropertyDefinition<?> pd) {
41          setId(pd.getId());
42          setDisplayName(pd.getDisplayName());
43          setLocalName(pd.getLocalName());
44          setQueryName(pd.getQueryName());
45      }
46  
47      /**
48       * Constructs a property from a list of values.
49       */
50      public PropertyImpl(PropertyDefinition<T> pd, List<T> values) {
51          if (pd == null) {
52              throw new IllegalArgumentException("Type must be set!");
53          }
54          if (values == null) {
55              throw new IllegalArgumentException("Value must be set!");
56          }
57          propertyDefinition = pd;
58          initialize(pd);
59          setValues(values);
60      }
61  
62      /**
63       * Copy constructor.
64       */
65      public PropertyImpl(Property<T> property) {
66          if (property == null) {
67              throw new IllegalArgumentException("Source must be set!");
68          }
69  
70          propertyDefinition = property.getDefinition();
71          initialize(property.getDefinition());
72          setValues(new ArrayList<T>(property.getValues()));
73      }
74  
75      public PropertyDefinition<T> getDefinition() {
76          return propertyDefinition;
77      }
78  
79      public PropertyType getType() {
80          return propertyDefinition.getPropertyType();
81      }
82  
83      @SuppressWarnings("unchecked")
84      public <U> U getValue() {
85          List<T> values = getValues();
86          if (propertyDefinition.getCardinality() == Cardinality.SINGLE) {
87              return values.size() == 0 ? null : (U) values.get(0);
88          } else {
89              return (U) values;
90          }
91      }
92  
93      public String getValueAsString() {
94          List<T> values = getValues();
95          if (values.size() == 0) {
96              return null;
97          }
98  
99          return formatValue(values.get(0));
100     }
101 
102     public String getValuesAsString() {
103         List<T> values = getValues();
104 
105         StringBuilder result = new StringBuilder();
106         for (T value : values) {
107             if (result.length() > 0) {
108                 result.append(", ");
109             }
110 
111             result.append(formatValue(value));
112         }
113 
114         return "[" + result.toString() + "]";
115     }
116 
117     private String formatValue(T value) {
118         String result;
119 
120         if (value == null) {
121             return null;
122         }
123 
124         if (value instanceof GregorianCalendar) {
125             result = ((GregorianCalendar) value).getTime().toString();
126         } else {
127             result = value.toString();
128         }
129 
130         return result;
131     }
132 
133     public boolean isMultiValued() {
134         return propertyDefinition.getCardinality() == Cardinality.MULTI;
135     }
136 }