Mutable.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      https://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.lang3.mutable;

  18. import java.util.function.Supplier;

  19. /**
  20.  * Provides mutable access to a value.
  21.  * <p>
  22.  * {@link Mutable} is used as a generic interface to the implementations in this package.
  23.  * </p>
  24.  * <p>
  25.  * A typical use case would be to enable a primitive or string to be passed to a method and allow that method to effectively change the value of the
  26.  * primitive/object. Another use case is to store a frequently changing primitive in a collection (for example a total in a map) without needing to create new
  27.  * Integer/Long wrapper objects.
  28.  * </p>
  29.  *
  30.  * @param <T> the type to wrap.
  31.  * @since 2.1
  32.  */
  33. public interface Mutable<T> extends Supplier<T> {

  34.     /**
  35.      * Gets the value of this mutable.
  36.      *
  37.      * @return the stored value.
  38.      * @since 3.18.0
  39.      */
  40.     @Override
  41.     default T get() {
  42.         return getValue();
  43.     }

  44.     /**
  45.      * Gets the value of this mutable.
  46.      *
  47.      * @return the stored value
  48.      * @deprecated Use {@link #get()}.
  49.      */
  50.     @Deprecated
  51.     T getValue();

  52.     /**
  53.      * Sets the value of this mutable.
  54.      *
  55.      * @param value the value to store
  56.      * @throws NullPointerException if the object is null and null is invalid.
  57.      * @throws ClassCastException   if the type is invalid.
  58.      */
  59.     void setValue(T value);
  60. }