View Javadoc

1   /*
2    * Copyright 2015 Data Archiving and Networked Services (an institute of
3    * Koninklijke Nederlandse Akademie van Wetenschappen), King's College London,
4    * Georg-August-Universitaet Goettingen Stiftung Oeffentlichen Rechts
5    *
6    * Licensed under the EUPL, Version 1.1 or – as soon they will be approved by
7    * the European Commission - subsequent versions of the EUPL (the "Licence");
8    * You may not use this work except in compliance with the Licence.
9    * You may obtain a copy of the Licence at:
10   *
11   * https://joinup.ec.europa.eu/software/page/eupl
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the Licence is distributed on an "AS IS" basis,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the Licence for the specific language governing
17   * permissions and limitations under the Licence.
18   */
19  
20  package eu.ehri.project.models.base;
21  
22  import com.tinkerpop.blueprints.Vertex;
23  import com.tinkerpop.frames.Property;
24  import com.tinkerpop.frames.VertexFrame;
25  import com.tinkerpop.frames.modules.javahandler.JavaHandler;
26  import com.tinkerpop.frames.modules.javahandler.JavaHandlerContext;
27  import eu.ehri.project.models.annotations.EntityType;
28  import eu.ehri.project.models.annotations.Mandatory;
29  
30  import java.util.Set;
31  
32  /**
33   * Base interface for all EHRI framed vertex types.
34   */
35  public interface Entity extends VertexFrame {
36  
37      /**
38       * Cast this frame to another type.
39       *
40       * @param cls the class of the framed type
41       * @param <T> the generic class
42       * @return the framed item as the new class
43       */
44      @JavaHandler
45      <T extends Entity> T as(Class<T> cls);
46  
47      /**
48       * Get the unique item id.
49       *
50       * @return id
51       */
52      @Mandatory
53      @Property(EntityType.ID_KEY)
54      String getId();
55  
56      /**
57       * Get the type key for this frame.
58       *
59       * @return type
60       */
61      @Mandatory
62      @Property(EntityType.TYPE_KEY)
63      String getType();
64  
65      /**
66       * Get an arbitrary property from the underlying vertex.
67       * @param key the property key
68       * @param <T> the property's type
69       * @return the property value, or null
70       */
71      @JavaHandler
72      <T> T getProperty(String key);
73  
74      @JavaHandler
75      <T> T getProperty(Enum<?> key);
76  
77  
78      /**
79       * Get the property keys from the underlying vertex.
80       *
81       * @return a set of string keys
82       */
83      @JavaHandler
84      Set<String> getPropertyKeys();
85  
86      abstract class Impl implements JavaHandlerContext<Vertex>, Accessible {
87  
88          @Override
89          public <T extends Entity> T as(Class<T> cls) {
90              return frame(it(), cls);
91          }
92  
93          @Override
94          public <T> T getProperty(String key) {
95              return it().getProperty(key);
96          }
97  
98          @Override
99          public <T> T getProperty(Enum<?> key) {
100             return it().getProperty(key.name());
101         }
102 
103         @Override
104         public Set<String> getPropertyKeys() {
105             return it().getPropertyKeys();
106         }
107     }
108 }