View Javadoc

1   /**
2    * Copyright (c) 2002-2011 "Neo Technology,"
3    * Network Engine for Objects in Lund AB [http://neotechnology.com]
4    *
5    * This file is part of Neo4j.
6    *
7    * Neo4j is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation, either version 3 of the License, or
10   * (at your option) any later version.
11   *
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19   */
20  package org.neo4j.server.rest.repr;
21  
22  import org.neo4j.graphdb.PropertyContainer;
23  import org.neo4j.server.helpers.PropertyTypeDispatcher;
24  
25  public final class PropertiesRepresentation extends MappingRepresentation
26  {
27      private final PropertyContainer entity;
28  
29      public PropertiesRepresentation( PropertyContainer entity )
30      {
31          super( RepresentationType.PROPERTIES );
32          this.entity = entity;
33      }
34  
35      public boolean isEmpty()
36      {
37          return !entity.getPropertyKeys().iterator().hasNext();
38      }
39  
40      @Override
41      protected void serialize( MappingSerializer serializer )
42      {
43          serialize( serializer.writer );
44      }
45  
46      void serialize( MappingWriter writer )
47      {
48          PropertyTypeDispatcher.consumeProperties( new Consumer( writer ), entity );
49      }
50  
51      private static class Consumer extends PropertyTypeDispatcher<String, Void>
52      {
53          private final MappingWriter writer;
54  
55          Consumer( MappingWriter serializer )
56          {
57              this.writer = serializer;
58          }
59  
60          @Override
61          protected Void dispatchBooleanProperty( boolean property, String param )
62          {
63              writer.writeBoolean( param, property );
64              return null;
65          }
66  
67          @Override
68          protected Void dispatchByteProperty( byte property, String param )
69          {
70              writer.writeInteger( RepresentationType.BYTE, param, property );
71              return null;
72          }
73  
74          @Override
75          protected Void dispatchCharacterProperty( char property, String param )
76          {
77              writer.writeInteger( RepresentationType.CHAR, param, property );
78              return null;
79          }
80  
81          @Override
82          protected Void dispatchDoubleProperty( double property, String param )
83          {
84              writer.writeFloatingPointNumber( RepresentationType.DOUBLE, param, property );
85              return null;
86          }
87  
88          @Override
89          protected Void dispatchFloatProperty( float property, String param )
90          {
91              writer.writeFloatingPointNumber( RepresentationType.FLOAT, param, property );
92              return null;
93          }
94  
95          @Override
96          protected Void dispatchIntegerProperty( int property, String param )
97          {
98              writer.writeInteger( RepresentationType.INTEGER, param, property );
99              return null;
100         }
101 
102         @Override
103         protected Void dispatchLongProperty( long property, String param )
104         {
105             writer.writeInteger( RepresentationType.LONG, param, property );
106             return null;
107         }
108 
109         @Override
110         protected Void dispatchShortProperty( short property, String param )
111         {
112             writer.writeInteger( RepresentationType.SHORT, param, property );
113             return null;
114         }
115 
116         @Override
117         protected Void dispatchStringProperty( String property, String param )
118         {
119             writer.writeString( param, property );
120             return null;
121         }
122 
123         @Override
124         protected Void dispatchStringArrayProperty( String[] property, String param )
125         {
126             ListWriter list = writer.newList( RepresentationType.STRING, param );
127             for ( String s : property )
128             {
129                 list.writeString( s );
130             }
131             list.done();
132             return null;
133         }
134 
135         @Override
136         @SuppressWarnings( "boxing" )
137         protected Void dispatchByteArrayProperty( PropertyArray<byte[], Byte> array, String param )
138         {
139             ListWriter list = writer.newList( RepresentationType.BYTE, param );
140             for ( Byte b : array )
141             {
142                 list.writeInteger( RepresentationType.BYTE, b );
143             }
144             list.done();
145             return null;
146         }
147 
148         @Override
149         @SuppressWarnings( "boxing" )
150         protected Void dispatchShortArrayProperty( PropertyArray<short[], Short> array,
151                 String param )
152         {
153             ListWriter list = writer.newList( RepresentationType.SHORT, param );
154             for ( Short s : array )
155             {
156                 list.writeInteger( RepresentationType.SHORT, s );
157             }
158             list.done();
159             return null;
160         }
161 
162         @Override
163         @SuppressWarnings( "boxing" )
164         protected Void dispatchCharacterArrayProperty( PropertyArray<char[], Character> array,
165                 String param )
166         {
167             ListWriter list = writer.newList( RepresentationType.CHAR, param );
168             for ( Character c : array )
169             {
170                 list.writeInteger( RepresentationType.CHAR, c );
171             }
172             list.done();
173             return null;
174         }
175 
176         @Override
177         @SuppressWarnings( "boxing" )
178         protected Void dispatchIntegerArrayProperty( PropertyArray<int[], Integer> array,
179                 String param )
180         {
181             ListWriter list = writer.newList( RepresentationType.INTEGER, param );
182             for ( Integer i : array )
183             {
184                 list.writeInteger( RepresentationType.INTEGER, i );
185             }
186             list.done();
187             return null;
188         }
189 
190         @Override
191         @SuppressWarnings( "boxing" )
192         protected Void dispatchLongArrayProperty( PropertyArray<long[], Long> array, String param )
193         {
194             ListWriter list = writer.newList( RepresentationType.LONG, param );
195             for ( Long j : array )
196             {
197                 list.writeInteger( RepresentationType.LONG, j );
198             }
199             list.done();
200             return null;
201         }
202 
203         @Override
204         @SuppressWarnings( "boxing" )
205         protected Void dispatchFloatArrayProperty( PropertyArray<float[], Float> array, String param )
206         {
207             ListWriter list = writer.newList( RepresentationType.FLOAT, param );
208             for ( Float f : array )
209             {
210                 list.writeFloatingPointNumber( RepresentationType.FLOAT, f );
211             }
212             list.done();
213             return null;
214         }
215 
216         @Override
217         @SuppressWarnings( "boxing" )
218         protected Void dispatchDoubleArrayProperty( PropertyArray<double[], Double> array,
219                 String param )
220         {
221             ListWriter list = writer.newList( RepresentationType.DOUBLE, param );
222             for ( Double d : array )
223             {
224                 list.writeFloatingPointNumber( RepresentationType.DOUBLE, d );
225             }
226             list.done();
227             return null;
228         }
229 
230         @Override
231         @SuppressWarnings( "boxing" )
232         protected Void dispatchBooleanArrayProperty( PropertyArray<boolean[], Boolean> array,
233                 String param )
234         {
235             ListWriter list = writer.newList( RepresentationType.BOOLEAN, param );
236             for ( Boolean z : array )
237             {
238                 list.writeBoolean( z );
239             }
240             list.done();
241             return null;
242         }
243     }
244 
245     public static Representation value( Object property )
246     {
247         return ValueRepresentation.property( property );
248     }
249 }