Utility class that provides a lazy initialization object wrapper. : Concurrent « Collections Data Structure « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Collections Data Structure » Concurrent 




Utility class that provides a lazy initialization object wrapper.
        
/*
  Copyright 2009 Tomer Gabel <[email protected]>

  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.


  ant-intellij-tasks project (http://code.google.com/p/ant-intellij-tasks/)

  $Id: Lazy.java 106 2009-09-30 02:07:29Z tomergabel $
*/

//package com.tomergabel.util;

import java.util.concurrent.Callable;

/**
 * Utility class that provides a lazy initialization object wrapper.
 * <p/>
 * To use this wrapper simply implement {@link java.util.concurrent.Callable#call()} and return the appropriate value.
 * Exceptions are propagated by {@link #get()} as {@link com.tomergabel.util.LazyInitializationException}s.
 * <p/>
 * <strong>Note: This wrapper is <em>not</em> thread-safe!</strong>
 *
 @param <T> The type wrapped by the lazy initializer.
 */
public abstract class Lazy<T> implements Callable<T> {
    /**
     * The actual value.
     */
    private T value = null;

    /**
     * Protected c'tor (to prevent direct initialization.
     */
    protected Lazy() {
    }

    /**
     * A private c'tor used to pre-cache the value. This is used by the convenience method {@link #from(Object)} to
     * create a "const" lazy initializer.
     *
     @param value The actual value.
     */
    private Lazyfinal T value ) {
        this.value = value;
    }

    /**
     * Retreives the lazy-loaded value, initializing it on the fly if necessary.
     *
     @return The lazy-loaded value.
     @throws LazyInitializationException An error has occurred while initializing the lazy-loaded value. Please see
     *                                     the exception {@link Exception#getCause() cause} for detials.
     */
    public T get() throws LazyInitializationException {
        try {
            return this.value == null this.value = this.call() ) this.value;
        catch Exception e ) {
            throw new LazyInitializationException);
        }
    }

    /**
     * Wraps a value with a pre-cached value. This is a convenience method intended to provide an easy way to specify
     * constant values to a lazy-loaded placeholder if necessary.
     *
     @param value The actual value.
     @param <T>   The type wrapped by the lazy initializer.
     @return {@link Lazy} instance for the specified value.
     */
    public static <T> Lazy<T> fromfinal T value ) {
        return new Lazy<T>value ) {
            @Override
            public T call() throws Exception {
                // Safety net, should never happen
                throw new IllegalStateException"Lazy initializer should be not called on lazy constants." );
            }
        };
    }

    /**
     * Wraps the specified callable with a {@link Lazy} instance. This is an alternative to extending this class.
     *
     @param initializer The initializer. Calling {@link #from} with {@literal null} for an argument will resolve here,
     *                    so specifying a {@literal null} initializer is the same as specifying a callable which returns
     *                    {@literal null}.
     @return {@link Lazy} instance for the specified initializer.
     */
    @SuppressWarnings( { "unchecked" } )
    public static <T> Lazy<T> fromfinal Callable<T> initializer ) {
        return new Lazy<T>() {
            @Override
            public T call() throws Exception {
                // Lazy.from( null ) will resolve here, so (as a convenience) we support
                // return of null values
                return initializer == null null : initializer.call();
            }
        };
    }
}
class LazyInitializationException extends Exception {
    public LazyInitializationExceptionfinal String message, final Throwable cause ) {
        supermessage, cause );
    }

    public LazyInitializationExceptionfinal Throwable cause ) {
        supercause );
    }
}

   
    
    
    
    
    
    
    
  














Related examples in the same category
1.A version of Hashtable supporting concurrency for both retrievals and updates
2.A version of Hashtable that supports mostly-concurrent reading, but exclusive writing
3.Synchronized Queue
4.Concurrent Doubly LinkedList
5.Returns the parent of the specified URI.
6.A daemon thread that continuously dequeues Runnable instances from a queue and executes them.
7.Lazy Loading Reference
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.