View Javadoc
1   /*
2    * Copyright (C) 2012 Alberto Irurueta Carro (alberto@irurueta.com)
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.irurueta.geometry.io;
17  
18  /**
19   * Property contained within a header element.
20   */
21  public class PropertyPLY {
22      /**
23       * Name of the property.
24       */
25      final String name;
26  
27      /**
28       * Property type (either scalar or list).
29       */
30      final PropertyTypePLY type;
31  
32      /**
33       * Data type of the value indicating length of the array for list properties.
34       */
35      final DataTypePLY lengthType;
36  
37      /**
38       * Data type for the values contained within this property.
39       */
40      final DataTypePLY valueType;
41  
42      /**
43       * Listener to read the value of this property contained within the byte
44       * read buffer and transform it into the appropriate data type for this
45       * property.
46       */
47      PLYReadValueFromBufferListener readValueFromBufferListener;
48  
49      /**
50       * Listener to read the appropriate amount of bytes from a PLY file
51       * corresponding to this property data type. The amount of bytes read are
52       * stored within the read buffer.
53       */
54      PLYReadValueFromStreamListener readValueFromStreamListener;
55  
56      /**
57       * Listener to read the length value of this property contained within the
58       * byte read buffer and transform it into the appropriate data type for this
59       * property.
60       */
61      PLYReadValueFromBufferListener readLengthValueFromBufferListener;
62  
63      /**
64       * Listener to read the appropriate amount of bytes from a PLY file
65       * corresponding to this property length data type. The amount of bytes
66       * read are stored within the read buffer.
67       */
68      PLYReadValueFromStreamListener readLengthValueFromStreamListener;
69  
70      /**
71       * Constructor.
72       *
73       * @param name      name of this property.
74       * @param valueType data type of the value of this property.
75       */
76      public PropertyPLY(final String name, final DataTypePLY valueType) {
77          this.name = name;
78          type = PropertyTypePLY.PROPERTY_PLY_SCALAR;
79          lengthType = null;
80          this.valueType = valueType;
81          readValueFromBufferListener = null;
82          readValueFromStreamListener = null;
83      }
84  
85      /**
86       * Constructor.
87       *
88       * @param name       name of this property.
89       * @param lengthType data type of the length value of this property.
90       * @param valueType  data type of the value of this property.
91       */
92      public PropertyPLY(final String name, final DataTypePLY lengthType, final DataTypePLY valueType) {
93          this.name = name;
94          type = PropertyTypePLY.PROPERTY_PLY_LIST;
95          this.lengthType = lengthType;
96          this.valueType = valueType;
97          readValueFromBufferListener = null;
98          readValueFromStreamListener = null;
99      }
100 
101     /**
102      * Returns name of this property.
103      *
104      * @return Name of this property.
105      * @throws NotAvailableException Raised if name has not been already
106      *                               provided.
107      */
108     public String getName() throws NotAvailableException {
109         if (!isNameAvailable()) {
110             throw new NotAvailableException();
111         }
112 
113         return name;
114     }
115 
116     /**
117      * Determines if name has already been provided and is ready for retrieval.
118      *
119      * @return True if name is available, false otherwise.
120      */
121     public boolean isNameAvailable() {
122         return (name != null);
123     }
124 
125     /**
126      * Property type (either scalar or list).
127      *
128      * @return property type.
129      */
130     public PropertyTypePLY getPropertyType() {
131         return type;
132     }
133 
134     /**
135      * Returns data type of the value indicating length of the array if this is
136      * a list property.
137      *
138      * @return data type of the value indicating length of the array if this is
139      * a list property.
140      * @throws NotAvailableException raised if property type has not yet been
141      *                               provided and is not available for retrieval.
142      */
143     public DataTypePLY getLengthType() throws NotAvailableException {
144         if (!isLengthTypeAvailable()) {
145             throw new NotAvailableException();
146         }
147         return lengthType;
148     }
149 
150     /**
151      * Determines whether length property type has been provided and is
152      * available for retrieval.
153      *
154      * @return True if length property type has been provided, false otherwise.
155      */
156     public boolean isLengthTypeAvailable() {
157         return (lengthType != null);
158     }
159 
160     /**
161      * Returns data type for the value contained within this property.
162      *
163      * @return data type for the value contained within this property.
164      * @throws NotAvailableException raised if property type has not yet been
165      *                               provided and is not available for retrieval.
166      */
167     public DataTypePLY getValueType() throws NotAvailableException {
168         if (!isValueTypeAvailable()) {
169             throw new NotAvailableException();
170         }
171         return valueType;
172     }
173 
174     /**
175      * Determines if data type for the value contained within this property has
176      * already been provided and is available for retrieval.
177      *
178      * @return True if data type for the value contained within this property
179      * is available for retrieval, false otherwise.
180      */
181     public boolean isValueTypeAvailable() {
182         return (valueType != null);
183     }
184 
185     /**
186      * Determines if this property is valid with the values that have already
187      * been provided.
188      *
189      * @return True if property is valid, false otherwise.
190      */
191     public boolean isValidProperty() {
192         return isNameAvailable() && isValueTypeAvailable();
193     }
194 
195     /**
196      * Converts this property to string representation ready to be written in
197      * the header of a PLY file.
198      *
199      * @return String representation of this property.
200      */
201     @Override
202     public String toString() {
203         // if element is invalid, return empty string
204         if (!isValidProperty()) {
205             return "";
206         }
207 
208         final var builder = new StringBuilder("property ");
209 
210         // depending whether property is scalar or list
211         if (type == PropertyTypePLY.PROPERTY_PLY_SCALAR) {
212             // add value data type
213             builder.append(valueType.getValue()).append(" ");
214 
215         } else if (type == PropertyTypePLY.PROPERTY_PLY_LIST && lengthType != null) {
216             // indicate it is a list by adding length data type and values
217             // data type
218             builder.append("list ").append(lengthType.getValue()).append(" ").append(valueType.getValue()).append(" ");
219         }
220         // add name
221         builder.append(name).append("\n");
222 
223         return builder.toString();
224     }
225 
226     /**
227      * Returns listener to read the value of this property contained within the
228      * byte read buffer and transforms it into the appropriate data type for
229      * this property.
230      *
231      * @return listener to read the value of this property.
232      * @throws NotAvailableException Raised if listener has not yet been
233      *                               provided and is not available for retrieval.
234      */
235     public PLYReadValueFromBufferListener getReadValueFromBufferListener() throws NotAvailableException {
236         if (!isReadValueFromBufferListenerAvailable()) {
237             throw new NotAvailableException();
238         }
239 
240         return readValueFromBufferListener;
241     }
242 
243     /**
244      * Sets listener to read the value of this property contained within the
245      * byte read buffer and transforms it into the appropriate data type for
246      * this property.
247      *
248      * @param listener listener to read the value of this property.
249      */
250     public void setReadValueFromBufferListener(final PLYReadValueFromBufferListener listener) {
251         readValueFromBufferListener = listener;
252     }
253 
254     /**
255      * Determines if listener to read the value of this property has been
256      * provided and is available for retrieval or not.
257      *
258      * @return True if listener is available, false otherwise.
259      */
260     public boolean isReadValueFromBufferListenerAvailable() {
261         return readValueFromBufferListener != null;
262     }
263 
264     /**
265      * Returns listener to read the appropriate amount of bytes from a PLY file
266      * corresponding to this property data type. The amount of bytes read are
267      * stored within the read buffer.
268      *
269      * @return listener to read the appropriate amount of bytes from a PLY file.
270      * @throws NotAvailableException Raised if listener has not yet been
271      *                               provided and is not available for retrieval.
272      */
273     public PLYReadValueFromStreamListener getReadValueFromStreamListener() throws NotAvailableException {
274         if (!isReadValueFromStreamListenerAvailable()) {
275             throw new NotAvailableException();
276         }
277 
278         return readValueFromStreamListener;
279     }
280 
281     /**
282      * Sets listener to read the appropriate amount of bytes from a PLY file
283      * corresponding to this property data type. The amount of bytes read are
284      * stored within the read buffer.
285      *
286      * @param listener listener to read the appropriate amount of bytes from a
287      *                 PLY file.
288      */
289     public void setReadValueFromStreamListener(final PLYReadValueFromStreamListener listener) {
290         readValueFromStreamListener = listener;
291     }
292 
293     /**
294      * Determines if listener to read the appropriate amount of bytes from a PLY
295      * file has been provided and is available for retrieval or not.
296      *
297      * @return True if listener is available, false otherwise.
298      */
299     public boolean isReadValueFromStreamListenerAvailable() {
300         return readValueFromStreamListener != null;
301     }
302 
303     /**
304      * Returns listener to read the length value of this property contained
305      * within the byte read buffer and transform it into the appropriate data
306      * type for this property.
307      *
308      * @return listener to read the length value of this property.
309      * @throws NotAvailableException Raised if listener has not yet been
310      *                               provided and is not available for retrieval.
311      */
312     public PLYReadValueFromBufferListener getReadLengthValueFromBufferListener() throws NotAvailableException {
313         if (!isReadLengthValueFromBufferListenerAvailable()) {
314             throw new NotAvailableException();
315         }
316 
317         return readLengthValueFromBufferListener;
318     }
319 
320     /**
321      * Sets listener to read the length value of this property contained within
322      * the byte read buffer and transform it into the appropriate data type for
323      * this property.
324      *
325      * @param listener listener to read the length value of this property.
326      */
327     public void setReadLengthValueFromBufferListener(final PLYReadValueFromBufferListener listener) {
328         readLengthValueFromBufferListener = listener;
329     }
330 
331     /**
332      * Determines if listener to read the length value of this property has been
333      * provided and is available for retrieval or not.
334      *
335      * @return True if listener is available, false otherwise.
336      */
337     public boolean isReadLengthValueFromBufferListenerAvailable() {
338         return readLengthValueFromBufferListener != null;
339     }
340 
341     /**
342      * Returns listener to read the appropriate amount of bytes from a PLY file
343      * corresponding to this property length data type. The amount of bytes
344      * read are stored within the read buffer.
345      *
346      * @return listener to read the appropriate amount of bytes from a PLY file.
347      * @throws NotAvailableException Raised if listener has not yet been
348      *                               provided and is not available for retrieval.
349      */
350     public PLYReadValueFromStreamListener getReadLengthValueFromStreamListener() throws NotAvailableException {
351         if (!isReadLengthValueFromStreamListenerAvailable()) {
352             throw new NotAvailableException();
353         }
354 
355         return readLengthValueFromStreamListener;
356     }
357 
358     /**
359      * Sets listener to read the appropriate amount of bytes from a PLY file
360      * corresponding to this property length data type. The amount of bytes
361      * read are stored within the read buffer.
362      *
363      * @param listener listener to read the appropriate amount of bytes from a
364      *                 PLY file.
365      */
366     public void setReadLengthValueFromStreamListener(final PLYReadValueFromStreamListener listener) {
367         readLengthValueFromStreamListener = listener;
368     }
369 
370     /**
371      * Determines if listener to read the appropriate amount of bytes from a PLY
372      * file has been provided and is available for retrieval or not.
373      *
374      * @return True if listener is available, false otherwise.
375      */
376     public boolean isReadLengthValueFromStreamListenerAvailable() {
377         return readLengthValueFromStreamListener != null;
378     }
379 }