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