home | career | drupal | java | mac | mysql | perl | scala | uml | unix

Groovy example source code file (ReferenceType.java)

This example Groovy source code file (ReferenceType.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Groovy tags/keywords

finalizable, finalizable, hardref, override, phantomref, reference, reference, referencequeue, referencequeue, t,v, tt, v, v, weakref

The Groovy ReferenceType.java source code

/*
 * Copyright 2003-2010 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;

import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;


public enum ReferenceType {
    SOFT {
        @Override
        protected <T,V extends Finalizable> Reference createReference(T value, V handler, ReferenceQueue queue) {
            return new SoftRef(value, handler, queue);
        }
    },
    WEAK {
        @Override
        protected <T,V extends Finalizable> Reference createReference(T value, V handler, ReferenceQueue queue) {
            return new WeakRef(value, handler, queue);
        }
    },
    PHANTOM {
        @Override
        protected <T,V extends Finalizable> Reference createReference(T value, V handler, ReferenceQueue queue) {
            return new PhantomRef(value, handler, queue);
        }            
    },
    HARD {
        @Override
        protected <T,V extends Finalizable> Reference createReference(T value, V handler, ReferenceQueue queue) {
            return new HardRef(value, handler, queue);
        }
    };
    protected abstract <T,V extends Finalizable> Reference createReference(T value, V handler, ReferenceQueue queue);
    
    private static class SoftRef<TT,V  extends Finalizable> extends SoftReference implements Reference {
        private final V handler;
        public SoftRef(TT referent, V handler, ReferenceQueue<? super TT> q) {
            super(referent, q);
            this.handler = handler;
        }
        public V getHandler() {
            return handler;
        }        
    }
    
    private static class WeakRef<TT,V  extends Finalizable> extends WeakReference implements Reference {
        private final V handler;
        public WeakRef(TT referent, V handler, ReferenceQueue<? super TT> q) {
            super(referent, q);
            this.handler = handler;
        }
        public V getHandler() {
            return handler;
        }            
    }
    
    private static class PhantomRef<TT,V  extends Finalizable> extends PhantomReference implements Reference {
        private final V handler;
        public PhantomRef(TT referent, V handler, ReferenceQueue<? super TT> q) {
            super(referent, q);
            this.handler = handler;
        }
        public V getHandler() {
            return handler;
        }            
    }
    
    private static class HardRef<TT,V extends Finalizable> implements Reference {
        private TT ref;
        private final V handler;
        public HardRef(TT referent, V handler, ReferenceQueue<? super TT> q) {
            this.ref = referent;
            this.handler = handler;
        }
        public V getHandler() {
            return handler;
        }
        public TT get() {
            return ref;
        }
        public void clear() {
            ref = null;
        }        
    }
    
}

Other Groovy examples (source code examples)

Here is a short list of links related to this Groovy ReferenceType.java source code file:

new blog posts

 

Copyright 1998-2013 Alvin Alexander, alvinalexander.com
All Rights Reserved.