PageRenderTime 22ms CodeModel.GetById 18ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/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
17package com.hazelcast.impl.concurrentmap;
18
19import com.hazelcast.nio.Data;
20
21import static com.hazelcast.nio.IOUtil.toObject;
22
23public class ValueHolder {
24    final Data data;
25    volatile Object value;
26
27    public ValueHolder(Data data) {
28        this.data = data;
29    }
30
31    public Data getData() {
32        return data;
33    }
34
35    @SuppressWarnings("NonPrivateFieldAccessedInSynchronizedContext")
36    public Object getValue() {
37        Object v = value;
38        if (v != null) return v;
39        //noinspection SynchronizeOnThis
40        synchronized (ValueHolder.this) {
41            value = toObject(data);
42            return value;
43        }
44    }
45
46    @Override
47    public boolean equals(Object o) {
48        if (this == o) return true;
49        if (o == null || getClass() != o.getClass()) return false;
50        ValueHolder that = (ValueHolder) o;
51        Object v = getValue();
52        return v.equals(that.getValue());
53    }
54
55    @Override
56    public int hashCode() {
57        final Object v = getValue();
58        return v != null ? v.hashCode() : Integer.MIN_VALUE;
59    }
60}