Coverage Report - org.apache.commons.lang3.mutable.MutableObject
 
Classes in this File Line Coverage Branch Coverage Complexity
MutableObject
100%
18/18
100%
10/10
2,143
 
 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  
  *      http://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  
 
 18  
 package org.apache.commons.lang3.mutable;
 19  
 
 20  
 import java.io.Serializable;
 21  
 
 22  
 /**
 23  
  * A mutable <code>Object</code> wrapper.
 24  
  * 
 25  
  * @param <T> the type to set and get 
 26  
  * @since 2.1
 27  
  * @version $Id: MutableObject.java 1562971 2014-01-30 21:23:18Z ggregory $
 28  
  */
 29  
 public class MutableObject<T> implements Mutable<T>, Serializable {
 30  
 
 31  
     /**
 32  
      * Required for serialization support.
 33  
      * 
 34  
      * @see java.io.Serializable
 35  
      */
 36  
     private static final long serialVersionUID = 86241875189L;
 37  
 
 38  
     /** The mutable value. */
 39  
     private T value;
 40  
 
 41  
     /**
 42  
      * Constructs a new MutableObject with the default value of <code>null</code>.
 43  
      */
 44  
     public MutableObject() {
 45  3
         super();
 46  3
     }
 47  
 
 48  
     /**
 49  
      * Constructs a new MutableObject with the specified value.
 50  
      * 
 51  
      * @param value  the initial value to store
 52  
      */
 53  
     public MutableObject(final T value) {
 54  39
         super();
 55  39
         this.value = value;
 56  39
     }
 57  
 
 58  
     //-----------------------------------------------------------------------
 59  
     /**
 60  
      * Gets the value.
 61  
      * 
 62  
      * @return the value, may be null
 63  
      */
 64  
     @Override
 65  
     public T getValue() {
 66  29
         return this.value;
 67  
     }
 68  
 
 69  
     /**
 70  
      * Sets the value.
 71  
      * 
 72  
      * @param value  the value to set
 73  
      */
 74  
     @Override
 75  
     public void setValue(final T value) {
 76  14
         this.value = value;
 77  14
     }
 78  
 
 79  
     //-----------------------------------------------------------------------
 80  
     /**
 81  
      * <p>
 82  
      * Compares this object against the specified object. The result is <code>true</code> if and only if the argument
 83  
      * is not <code>null</code> and is a <code>MutableObject</code> object that contains the same <code>T</code>
 84  
      * value as this object.
 85  
      * </p>
 86  
      * 
 87  
      * @param obj  the object to compare with, <code>null</code> returns <code>false</code>
 88  
      * @return  <code>true</code> if the objects are the same;
 89  
      *          <code>true</code> if the objects have equivalent <code>value</code> fields;
 90  
      *          <code>false</code> otherwise.
 91  
      */
 92  
     @Override
 93  
     public boolean equals(final Object obj) {
 94  12
         if (obj == null) {
 95  1
             return false;
 96  
         }
 97  11
         if (this == obj) {
 98  4
             return true;
 99  
         }
 100  7
         if (this.getClass() == obj.getClass()) {
 101  5
             final MutableObject<?> that = (MutableObject<?>) obj;
 102  5
             return this.value.equals(that.value);
 103  
         }
 104  2
         return false;
 105  
     }
 106  
 
 107  
     /**
 108  
      * Returns the value's hash code or <code>0</code> if the value is <code>null</code>.
 109  
      * 
 110  
      * @return the value's hash code or <code>0</code> if the value is <code>null</code>.
 111  
      */
 112  
     @Override
 113  
     public int hashCode() {
 114  10
         return value == null ? 0 : value.hashCode();
 115  
     }
 116  
 
 117  
     //-----------------------------------------------------------------------
 118  
     /**
 119  
      * Returns the String value of this mutable.
 120  
      * 
 121  
      * @return the mutable value as a string
 122  
      */
 123  
     @Override
 124  
     public String toString() {
 125  20
         return value == null ? "null" : value.toString();
 126  
     }
 127  
 
 128  
 }