001    /*
002     * The MIT License
003     * Copyright (c) 2012 Microsoft Corporation
004     *
005     * Permission is hereby granted, free of charge, to any person obtaining a copy
006     * of this software and associated documentation files (the "Software"), to deal
007     * in the Software without restriction, including without limitation the rights
008     * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
009     * copies of the Software, and to permit persons to whom the Software is
010     * furnished to do so, subject to the following conditions:
011     *
012     * The above copyright notice and this permission notice shall be included in
013     * all copies or substantial portions of the Software.
014     *
015     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
016     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
017     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
018     * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
019     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
020     * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
021     * THE SOFTWARE.
022     */
023    
024    package microsoft.exchange.webservices.data.core;
025    
026    /**
027     * Wrapper class for lazy members. Does lazy initialization of member on first
028     * access.
029     *
030     * @param <T> Type of the lazy member
031     *            <p>
032     *            If we find ourselves creating a whole bunch of these in our code,
033     *            we need to rethink this. Each lazy member holds the actual member
034     *            and a delegate. That can turn into a whole lot of overhead
035     *            </p>
036     */
037    public class LazyMember<T> {
038    
039      /**
040       * The lazy member.
041       */
042      private volatile T lazyMember;
043    
044      /**
045       * The lazy implementation.
046       */
047      private final ILazyMember<T> lazyImplementation;
048    
049      /**
050       * Public accessor for the lazy member. Lazy initializes the member on first
051       * access
052       *
053       * @return the member
054       */
055      public T getMember() {
056        T result = this.lazyMember;
057        if (result == null) {  // first check (no locking)
058          synchronized (this) {
059            result = this.lazyMember;
060            if (result == null) { // second check (with locking)
061              this.lazyMember = result = lazyImplementation.createInstance();
062            }
063          }
064        }
065        return result;
066      }
067    
068      /**
069       * Constructor.
070       *
071       * @param lazyImplementation The initialization delegate to call for the item on first
072       *                           access
073       */
074      public LazyMember(ILazyMember<T> lazyImplementation) {
075        this.lazyImplementation = lazyImplementation;
076      }
077    }