6
\$\begingroup\$

Generally, I structure small threadsafe immutable objects like this:

public class SmallObject {
 private final String state;
 public SmallObject(final String state) {
 this.state = state;
 }
 // ...
}

And then wire these up in Spring like this:

<bean name="SmallObjectForThisThing" class="my.package.SmallObject">
 <constructor-arg name="state" value="in practice this is usually a ref"/>
</bean>

However, this leads to complications with circular dependencies. To keep the immutability when this happens, I use a "freeze" pattern, where the variables are set once. This is what I want reviewed:

public class SmallObject {
 private String state = null;
 public void setState(String state) {
 if (this.state != null) {
 throw new IllegalStateException("state already set: '" + state + "'.");
 }
 this.state = state;
 }
 private void ensureInitialized() {
 if (this.state == null) {
 throw new IllegalStateException(
 "state must be set before this instance is used."
 );
 }
 }
 // ... For every additional method on the object, I call 
 // ensureInitialized() first.
}

And then wire them up like this:

<bean name="SmallObjectForThisThing" class="my.package.SmallObject">
 <property name="state" value="in practice this is usually a ref"/>
</bean>
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Jan 4, 2013 at 19:23
\$\endgroup\$
1
  • 2
    \$\begingroup\$ I do not understand the purpose, an opinion about this solution depends heavily on the circumstances. This approach will generate a lot of problems if it comes to threadsafety. And it is not really immutable. Perhaps, it could be a better way to invest some time to solve the "circular dependencies" problem. \$\endgroup\$ Commented Jan 8, 2013 at 11:36

1 Answer 1

7
\$\begingroup\$

This class should instead use an AtomicReference to ensure the state is kept valid. Alternatively, you should incorporate thread-safe handling of the String.

Consider:

private final AtomicReference<String> stateref = new AtomicReference<String>();
public void setState(final String state) {
 // only one initializer will succeed (assuming state is not null)...
 if (!stateref.compareAndSet(null, state)) {
 throw new IllegalStateException("state already set: '" + state + "'.");
 }
}
private void ensureInitialized() {
 if (stateref.get() == null) {
 throw new IllegalStateException(
 "state must be set before this instance is used."
 );
 }
}

This pattern ensures usage is consistent, there can be only one initialization of the instance, and that any thread-unsafe practices are handled well.

answered Dec 9, 2013 at 6:15
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.