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.bean.jsonapi;
018
019import static org.apache.juneau.commons.utils.CollectionUtils.*;
020import static org.apache.juneau.commons.utils.Shorts.*;
021
022import java.util.*;
023
024import org.apache.juneau.marshall.*;
025import org.apache.juneau.marshall.marshaller.*;
026
027/**
028 * Represents a JSON:API Resource Identifier Object as defined by
029 * <a href="https://jsonapi.org/format/#document-resource-identifier-objects">JSON:API v1.1 &#167; Resource Identifier Objects</a>.
030 *
031 * <p>
032 * A slim form of {@link JsonApiResource} used inside relationship {@code data} linkage. Carries only {@code type},
033 * {@code id}, and an optional {@code meta} bag. {@code type} is a plain {@code String} for the same reasons
034 * documented on {@link JsonApiResource#getType()}.
035 *
036 * <h5 class='section'>See Also:</h5><ul>
037 *   <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauBeanJsonApi">juneau-bean-jsonapi</a>
038 * </ul>
039 */
040@Marshalled
041public class JsonApiResourceIdentifier {
042
043   private String type;
044   private String id;
045   private Map<String,Object> meta;
046
047   /**
048    * Default constructor.
049    */
050   public JsonApiResourceIdentifier() {}
051
052   /**
053    * Convenience constructor.
054    *
055    * @param type The entity-type name.  Can be <jk>null</jk> to leave the property unset.
056    * @param id The identifier value.  Can be <jk>null</jk> to leave the property unset.
057    */
058   public JsonApiResourceIdentifier(String type, String id) {
059      this.type = type;
060      this.id = id;
061   }
062
063   /**
064    * Bean property getter:  <property>type</property>.
065    *
066    * @return The value of the <property>type</property> property, or <jk>null</jk> if it is not set.
067    */
068   public String getType() { return type; }
069
070   /**
071    * Bean property setter:  <property>type</property>.
072    *
073    * @param value The new value for this property.  Can be <jk>null</jk> to unset the property.
074    * @return This object.
075    */
076   public JsonApiResourceIdentifier setType(String value) {
077      type = value;
078      return this;
079   }
080
081   /**
082    * Bean property getter:  <property>id</property>.
083    *
084    * @return The value of the <property>id</property> property, or <jk>null</jk> if it is not set.
085    */
086   public String getId() { return id; }
087
088   /**
089    * Bean property setter:  <property>id</property>.
090    *
091    * @param value The new value for this property.  Can be <jk>null</jk> to unset the property.
092    * @return This object.
093    */
094   public JsonApiResourceIdentifier setId(String value) {
095      id = value;
096      return this;
097   }
098
099   /**
100    * Bean property getter:  <property>meta</property>.
101    *
102    * @return The value of the <property>meta</property> property, or <jk>null</jk> if it is not set.
103    */
104   public Map<String,Object> getMeta() { return u(meta); }
105
106   /**
107    * Bean property setter:  <property>meta</property>.
108    *
109    * @param value The new value for this property.  Can be <jk>null</jk> to unset the property.
110    * @return This object.
111    */
112   public JsonApiResourceIdentifier setMeta(Map<String,Object> value) {
113      meta = value;
114      return this;
115   }
116
117   /**
118    * Convenience method to add a single meta entry.
119    *
120    * @param name The meta key.  Can be <jk>null</jk> ({@link LinkedHashMap} tolerates a <jk>null</jk> key).
121    * @param value The meta value.  Can be <jk>null</jk> (stored as <jk>null</jk>).
122    * @return This object.
123    */
124   public JsonApiResourceIdentifier putMeta(String name, Object value) {
125      if (meta == null)
126         meta = map();
127      meta.put(name, value);
128      return this;
129   }
130
131   @Override /* Overridden from Object */
132   public String toString() {
133      return Json.of(this);
134   }
135}