PageRenderTime 12ms CodeModel.GetById 2ms app.highlight 7ms RepoModel.GetById 2ms app.codeStats 0ms

/hazelcast-hibernate/src/main/java/com/hazelcast/hibernate/VersionAwareMergePolicy.java

https://bitbucket.org/gabral6_gmailcom/hazelcast
Java | 55 lines | 36 code | 4 blank | 15 comment | 17 complexity | 9d34b4058caa90fa6cde30af1919a2c7 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.hibernate;
18
19import com.hazelcast.core.MapEntry;
20import com.hazelcast.impl.base.DataRecordEntry;
21import com.hazelcast.merge.MergePolicy;
22import org.hibernate.cache.entry.CacheEntry;
23
24public class VersionAwareMergePolicy implements MergePolicy {
25    public static final String NAME = "hz.HIBERNATE_VERSION_AWARE";
26
27    @SuppressWarnings({"rawtypes", "unchecked"})
28    public Object merge(String mapName, MapEntry mergingEntry, MapEntry existingEntry) {
29        DataRecordEntry mergingDataEntry = (DataRecordEntry) mergingEntry;
30        if (!mergingDataEntry.isValid()) {
31            return REMOVE_EXISTING;
32        } else {
33            final Object existingObject = existingEntry != null ? existingEntry.getValue() : null;
34            final Object mergingObject = mergingEntry.getValue();
35            if (existingObject != null && existingObject instanceof CacheEntry
36                    && mergingObject != null && mergingObject instanceof CacheEntry) {
37                final CacheEntry existing = (CacheEntry) existingObject;
38                final CacheEntry merging = (CacheEntry) mergingObject;
39                final Object mergingVersionObject = merging.getVersion();
40                final Object existingVersionObject = existing.getVersion();
41                if (mergingVersionObject != null && existingVersionObject != null
42                        && mergingVersionObject instanceof Comparable && existingVersionObject instanceof Comparable) {
43                    final Comparable mergingVersion = (Comparable) mergingVersionObject;
44                    final Comparable existingVersion = (Comparable) existingVersionObject;
45                    if (mergingVersion.compareTo(existingVersion) > 0) {
46                        return mergingDataEntry.getValueData();
47                    } else {
48                        return ((DataRecordEntry) existingEntry).getValueData();
49                    }
50                }
51            }
52            return mergingDataEntry.getValueData();
53        }
54    }
55}