001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.rest.helper;
014
015import static org.apache.juneau.internal.ObjectUtils.*;
016
017import org.apache.juneau.annotation.*;
018import org.apache.juneau.html.annotation.*;
019import org.apache.juneau.http.annotation.*;
020import org.apache.juneau.jsonschema.annotation.Schema;
021
022/**
023 * Shortcut label for child resources.
024 *
025 * <p>
026 * Typically used in router resources.
027 *
028 * <h5 class='section'>Example:</h5>
029 * <p class='bcode w800'>
030 *    <jk>new</jk> ResourceLink(<js>"httpTool"</js>, <js>"HTTP request test client"</js>);
031 * </p>
032 *
033 * <ul class='seealso'>
034 *    <li class='link'>{@doc RestmPredefinedHelperBeans}
035 * </ul>
036 */
037@Bean(bpi="name,description", fluentSetters=true)
038@Response(schema=@Schema(ignore=true))
039public final class ResourceDescription implements Comparable<ResourceDescription> {
040
041   private String name, uri, description;
042
043   /**
044    * Constructor for when the name and uri are the same.
045    *
046    * @param name The name of the child resource.
047    * @param description The description of the child resource.
048    */
049   public ResourceDescription(String name, String description) {
050      this.name = name;
051      this.uri = name;
052      this.description = description;
053   }
054
055   /**
056    * Constructor for when the name and uri are different.
057    *
058    * @param name The name of the child resource.
059    * @param uri The uri of the child resource.
060    * @param description The description of the child resource.
061    */
062   public ResourceDescription(String name, String uri, String description) {
063      this.name = name;
064      this.uri = uri;
065      this.description = description;
066   }
067
068   /** No-arg constructor.  Used for JUnit testing of OPTIONS pages. */
069   public ResourceDescription() {}
070
071   /**
072    * Returns the name field on this label.
073    *
074    * @return The name.
075    */
076   @Html(link="servlet:/{uri}")
077   public String getName() {
078      return name;
079   }
080
081   /**
082    * Returns the uri on this label.
083    *
084    * @return The name.
085    */
086   public String getUri() {
087      return uri == null ? name : uri;
088   }
089
090   /**
091    * Sets the name field on this label to a new value.
092    *
093    * @param name The new name.
094    * @return This object (for method chaining).
095    */
096   public ResourceDescription name(String name) {
097      this.name = name;
098      return this;
099   }
100
101   /**
102    * Returns the description field on this label.
103    *
104    * @return The description.
105    */
106   public String getDescription() {
107      return description;
108   }
109
110   /**
111    * Sets the description field on this label to a new value.
112    *
113    * @param description The new description.
114    * @return This object (for method chaining).
115    */
116   public ResourceDescription description(String description) {
117      this.description = description;
118      return this;
119   }
120
121   /**
122    * Sets the uri field on this label to a new value.
123    *
124    * @param uri The new uri.
125    * @return This object (for method chaining).
126    */
127   public ResourceDescription uri(String uri) {
128      this.uri = uri;
129      return this;
130   }
131
132   @Override /* Comparable */
133   public int compareTo(ResourceDescription o) {
134      return getName().toString().compareTo(o.getName().toString());
135   }
136
137   @Override /* Object */
138   public boolean equals(Object o) {
139      return (o instanceof ResourceDescription) && eq(this, (ResourceDescription)o, (x,y)->eq(x.getName(), y.getName()));
140   }
141
142   @Override /* Object */
143   public int hashCode() {
144      return getName().hashCode();
145   }
146}