001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.wicket.util; 018 019import java.util.function.Supplier; 020 021import org.apache.wicket.util.io.IClusterable; 022 023/** 024 * An abstraction for lazy-initializing values. Guarantees only a single instance of the value is 025 * created. 026 * 027 * Initialized value <strong>WILL NOT</strong> be serialized, and will be recreated upon 028 * de-serialization. 029 * 030 * @author igor 031 * @param <T> 032 * type of value 033 */ 034public abstract class LazyInitializer<T> implements Supplier<T>, IClusterable 035{ 036 private static final long serialVersionUID = 1L; 037 038 private transient volatile T instance = null; 039 040 @Override 041 public T get() 042 { 043 if (instance == null) 044 { 045 synchronized (this) 046 { 047 if (instance == null) 048 { 049 instance = createInstance(); 050 } 051 } 052 } 053 return instance; 054 } 055 056 /** 057 * Creates the lazy value 058 * 059 * @return new instance of the value 060 */ 061 protected abstract T createInstance(); 062}