/hazelcast/src/main/java/com/hazelcast/impl/concurrentmap/ValueHolder.java

https://bitbucket.org/gabral6_gmailcom/hazelcast · Java · 60 lines · 35 code · 9 blank · 16 comment · 9 complexity · c673a6764c9e91f4d4b123a57ede5875 MD5 · raw file

  1. /*
  2. * Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.hazelcast.impl.concurrentmap;
  17. import com.hazelcast.nio.Data;
  18. import static com.hazelcast.nio.IOUtil.toObject;
  19. public class ValueHolder {
  20. final Data data;
  21. volatile Object value;
  22. public ValueHolder(Data data) {
  23. this.data = data;
  24. }
  25. public Data getData() {
  26. return data;
  27. }
  28. @SuppressWarnings("NonPrivateFieldAccessedInSynchronizedContext")
  29. public Object getValue() {
  30. Object v = value;
  31. if (v != null) return v;
  32. //noinspection SynchronizeOnThis
  33. synchronized (ValueHolder.this) {
  34. value = toObject(data);
  35. return value;
  36. }
  37. }
  38. @Override
  39. public boolean equals(Object o) {
  40. if (this == o) return true;
  41. if (o == null || getClass() != o.getClass()) return false;
  42. ValueHolder that = (ValueHolder) o;
  43. Object v = getValue();
  44. return v.equals(that.getValue());
  45. }
  46. @Override
  47. public int hashCode() {
  48. final Object v = getValue();
  49. return v != null ? v.hashCode() : Integer.MIN_VALUE;
  50. }
  51. }