View Javadoc
1   /*
2    * #%L
3    * wcm.io
4    * %%
5    * Copyright (C) 2015 wcm.io
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package io.wcm.caravan.hal.docs.impl.model;
21  
22  import java.util.ArrayList;
23  import java.util.Collection;
24  import java.util.List;
25  import java.util.SortedSet;
26  import java.util.TreeSet;
27  
28  import org.apache.commons.lang3.StringUtils;
29  
30  /**
31   * Describes a HAL link relation.
32   */
33  public class LinkRelation implements Comparable<LinkRelation> {
34  
35    private String rel;
36    private String shortDescription;
37    private String descriptionMarkup;
38    private String jsonSchemaRef;
39    private List<ResourceRef> resourceRefs = new ArrayList<>();
40    private SortedSet<LinkRelationRef> linkRelationRefs = new TreeSet<>();
41  
42    public String getRel() {
43      return this.rel;
44    }
45  
46    public void setRel(String rel) {
47      this.rel = rel;
48    }
49  
50    public String getShortDescription() {
51      return this.shortDescription;
52    }
53  
54    public void setShortDescription(String shortDescription) {
55      this.shortDescription = shortDescription;
56    }
57  
58    public String getDescriptionMarkup() {
59      return this.descriptionMarkup;
60    }
61  
62    public void setDescriptionMarkup(String descriptionMarkup) {
63      this.descriptionMarkup = descriptionMarkup;
64    }
65  
66    public String getJsonSchemaRef() {
67      return this.jsonSchemaRef;
68    }
69  
70    public void setJsonSchemaRef(String jsonSchemaRef) {
71      this.jsonSchemaRef = jsonSchemaRef;
72    }
73  
74    /**
75     * @return Get embedded resoruces
76     */
77    public Collection<ResourceRef> getResourceRefs() {
78      return resourceRefs;
79    }
80  
81    /**
82     * @param refName Resource name
83     * @param refDescription Resource description
84     * @param refJsonSchemaRef JSON schema reference
85     */
86    public void addResourceRef(String refName, String refDescription, String refJsonSchemaRef) {
87      ResourceRef ref = new ResourceRef();
88      ref.setName(refName);
89      ref.setShortDescription(refDescription);
90      ref.setJsonSchemaRef(refJsonSchemaRef);
91      this.resourceRefs.add(ref);
92    }
93  
94    /**
95     * @return Get contained link relations
96     */
97    public Collection<LinkRelationRef> getLinkRelationRefs() {
98      return linkRelationRefs;
99    }
100 
101   /**
102    * @param refRel Link relation name.
103    * @param refDescription Optional description for describing the link relation in context of the parent link relation.
104    */
105   public void addLinkRelationRef(String refRel, String refDescription) {
106     LinkRelationRef ref = new LinkRelationRef();
107     ref.setRel(refRel);
108     ref.setShortDescription(refDescription);
109     this.linkRelationRefs.add(ref);
110   }
111 
112   @Override
113   public int hashCode() {
114     return rel.hashCode();
115   }
116 
117   @Override
118   public boolean equals(Object obj) {
119     if (!(obj instanceof LinkRelation)) {
120       return false;
121     }
122     return StringUtils.equals(rel, ((LinkRelation)obj).rel);
123   }
124 
125   @Override
126   public int compareTo(LinkRelation o) {
127     return StringUtils.defaultString(rel).compareTo(o.getRel());
128   }
129 
130 }