All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.codehaus.groovy.util.ManagedDoubleKeyMap Maven / Gradle / Ivy

There is a newer version: 3.0.21
Show newest version
/*
 * Copyright 2003-2007 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.codehaus.groovy.util;


public class ManagedDoubleKeyMap extends AbstractConcurrentDoubleKeyMap {
    public ManagedDoubleKeyMap(ReferenceBundle bundle) {
        super(bundle);
    }
    
    protected AbstractConcurrentDoubleKeyMap.Segment createSegment(Object segmentInfo, int cap) {
        ReferenceBundle bundle = (ReferenceBundle) segmentInfo;
        return new Segment(bundle, cap);
    }

    static class Segment extends AbstractConcurrentDoubleKeyMap.Segment{
        private ReferenceBundle bundle;
        public Segment(ReferenceBundle bundle, int cap) {
            super(cap);
            this.bundle = bundle;
        }

        protected AbstractConcurrentDoubleKeyMap.Entry createEntry(K1 key1, K2 key2, int hash) {
            return new EntryWithValue(bundle, key1, key2, hash, this);
        }
    }

    static class Ref extends ManagedReference {
        final Entry entry;
        public Ref(ReferenceBundle bundle, K referent, Entry entry) {
            super(bundle, referent);
            this.entry = entry;
        }

        public void finalizeRef() {
            this.entry.clean();
        }
    }

    public static class Entry implements AbstractConcurrentDoubleKeyMap.Entry {
        private final int hash;
        final Ref ref1;
        final Ref ref2;
        final Segment segment;

        public Entry(ReferenceBundle bundle, K1 key1, K2 key2, int hash, Segment segment) {
            this.hash = hash;
            this.segment = segment;
            ref1 = new Ref(bundle, key1, this);
            ref2 = new Ref(bundle, key2, this);
        }

        public boolean isValid() {
            return ref1.get() != null && ref2.get () != null;
        }

        public boolean isEqual(K1 key1, K2 key2, int hash) {
            return this.hash == hash && ref1.get() == key1 && ref2.get() == key2;
        }

        public V getValue() {
            return (V)this;
        }

        public void setValue(V value) {
        }

        public int getHash() {
            return hash;
        }

        public void clean() {
            segment.removeEntry(this);
            ref1.clear();
            ref2.clear();
        }
    }

    private static class EntryWithValue extends Entry {
        private V value;

        public EntryWithValue(ReferenceBundle bundle, K1 key1, K2 key2, int hash, Segment segment) {
            super(bundle, key1, key2, hash, segment);
        }

        public V getValue() {
            return value;
        }

        public void setValue(V value) {
            this.value = value;
        }

        public void clean() {
            super.clean();
            value = null;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy