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.utils;
014
015import java.util.*;
016
017/**
018 * Represents a settable object.
019 *
020 * Typically passed as method parameters to provide by-reference support.
021 *
022 * <p class='bcode w800'>
023 *    Mutable&lt;String&gt; m = Mutable.<jsm>create</jsm>(String.<jk>class</jk>);
024 *    callSomeMethodThatSetsValue(m);
025 *    String val = m.get();
026 * </p>
027 *
028 * <ul class='notes'>
029 *    <li>
030 *       This class is not thread safe.
031 *    <li>
032 *       This object can be used as hashmap keys.
033 * </ul>
034 *
035 * @param <T> The inner object type.
036 */
037public class Mutable<T> {
038
039   private T value;
040
041   /**
042    * Creates an empty mutable.
043    *
044    * @param <T> The inner object type.
045    * @param c The inner object type.
046    * @return The new mutable object.
047    */
048   public static <T> Mutable<T> create(Class<T> c) {
049      return new Mutable<>();
050   }
051
052   /**
053    * Creates an empty mutable.
054    *
055    * @param <T> The inner object type.
056    * @return The new mutable object.
057    */
058   public static <T> Mutable<T> create() {
059      return new Mutable<>();
060   }
061
062   /**
063    * Creates a mutable initialized with the specified object.
064    *
065    * @param <T> The inner object type.
066    * @param t The inner object.
067    * @return The new mutable object.
068    */
069   public static <T> Mutable<T> of(T t) {
070      return new Mutable<>(t);
071   }
072
073   /**
074    * Creates an empty mutable.
075    */
076   public Mutable() {}
077
078   /**
079    * Creates a mutable initialized with the specified object.
080    *
081    * @param t The inner object.
082    */
083   public Mutable(T t) {
084      this.value = t;
085   }
086
087   /**
088    * Returns the inner object.
089    *
090    * @return The inner object, or <jk>null</jk> if empty.
091    */
092   public T get() {
093      return value;
094   }
095
096   /**
097    * Sets the inner object.
098    *
099    * @param t The inner object.
100    * @return This object (for method chaining).
101    */
102   public Mutable<T> set(T t) {
103      this.value = t;
104      return this;
105   }
106
107   /**
108    * Returns <jk>true</jk> if inner object is set.
109    *
110    * @return <jk>true</jk> if inner object is set.
111    */
112   public boolean isSet() {
113      return value != null;
114   }
115
116   @Override /* Object */
117   public boolean equals(Object o) {
118      return Objects.equals(o, value);
119   }
120
121   @Override /* Object */
122   public int hashCode() {
123      return value == null ? 0 : value.hashCode();
124   }
125
126   @Override /* Object */
127   public String toString() {
128      return value == null ? "null" : value.toString();
129   }
130}