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.concurrent;
18
19 import java.util.Objects;
20
21 /**
22 * A very simple implementation of the {@link ConcurrentInitializer} interface
23 * which always returns the same object.
24 *
25 * <p>
26 * An instance of this class is passed a reference to an object when it is
27 * constructed. The {@link #get()} method just returns this object. No
28 * synchronization is required.
29 * </p>
30 * <p>
31 * This class is useful for instance for unit testing or in cases where a
32 * specific object has to be passed to an object which expects a
33 * {@link ConcurrentInitializer}.
34 * </p>
35 *
36 * @param <T> The type of the object managed by this initializer
37 * @since 3.0
38 */
39 public class ConstantInitializer<T> implements ConcurrentInitializer<T> {
40
41 /** Constant for the format of the string representation. */
42 private static final String FMT_TO_STRING = "ConstantInitializer@%d [ object = %s ]";
43
44 /** Stores the managed object. */
45 private final T object;
46
47 /**
48 * Creates a new instance of {@link ConstantInitializer} and initializes it
49 * with the object to be managed. The {@code get()} method will always
50 * return the object passed here. This class does not place any restrictions
51 * on the object. It may be {@code null}, then {@code get()} will return
52 * {@code null}, too.
53 *
54 * @param obj The object to be managed by this initializer
55 */
56 public ConstantInitializer(final T obj) {
57 object = obj;
58 }
59
60 /**
61 * Compares this object with another one. This implementation returns
62 * <strong>true</strong> if and only if the passed in object is an instance of
63 * {@link ConstantInitializer} which refers to an object equals to the
64 * object managed by this instance.
65 *
66 * @param obj The object to compare to
67 * @return A flag whether the objects are equal
68 */
69 @Override
70 public boolean equals(final Object obj) {
71 if (this == obj) {
72 return true;
73 }
74 if (!(obj instanceof ConstantInitializer<?>)) {
75 return false;
76 }
77
78 final ConstantInitializer<?> c = (ConstantInitializer<?>) obj;
79 return Objects.equals(getObject(), c.getObject());
80 }
81
82 /**
83 * Gets the object managed by this initializer. This implementation just
84 * returns the object passed to the constructor.
85 *
86 * @return The object managed by this initializer
87 * @throws ConcurrentException Thrown if an error occurs.
88 */
89 @Override
90 public T get() throws ConcurrentException {
91 return getObject();
92 }
93
94 /**
95 * Gets the object passed to the constructor. This is the same object returned by {@code get()}, but this method does not declare a checked exception.
96 *
97 * @return The object managed by this initializer
98 */
99 public final T getObject() {
100 return object;
101 }
102
103 /**
104 * Returns a hash code for this object. This implementation returns the hash
105 * code of the managed object.
106 *
107 * @return A hash code for this object
108 */
109 @Override
110 public int hashCode() {
111 return Objects.hashCode(object);
112 }
113
114 /**
115 * Tests whether this initializer is initialized. Always returns true because initialization occurs during construction.
116 *
117 * @return true.
118 * @since 3.14.0
119 */
120 public boolean isInitialized() {
121 return true;
122 }
123
124 /**
125 * Returns a string representation for this object. This string also
126 * contains a string representation of the object managed by this
127 * initializer.
128 *
129 * @return A string for this object
130 */
131 @Override
132 public String toString() {
133 return String.format(FMT_TO_STRING, Integer.valueOf(System.identityHashCode(this)), getObject());
134 }
135 }