001/** 002 * Get more info at : www.jrebirth.org . 003 * Copyright JRebirth.org © 2011-2014 004 * Contact : sebastien.bordes@jrebirth.org 005 * 006 * Licensed under the Apache License, Version 2.0 (the "License"); 007 * you may not use this file except in compliance with the License. 008 * You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.jrebirth.af.core.wave; 019 020import java.util.Collections; 021import java.util.HashMap; 022import java.util.Map; 023import java.util.concurrent.atomic.AtomicInteger; 024 025import org.jrebirth.af.api.wave.contract.WaveType; 026 027/** 028 * The Class WaveTypeRegistry used to store all {@link WaveType}. 029 * 030 * @author Sébastien Bordes 031 */ 032public final class WaveTypeRegistry { 033 034 /** The generator of unique id. */ 035 public static final AtomicInteger idGenerator = new AtomicInteger(); 036 037 /** Map that store WaveType used according to their unique identifier. */ 038 public static final Map<String, WaveType> waveTypeMap = Collections.synchronizedMap(new HashMap<String, WaveType>()); 039 040 /** 041 * Private Constructor. 042 */ 043 private WaveTypeRegistry() { 044 // Nothing to do 045 } 046 047 /** 048 * Return the next free unique identifier. 049 * 050 * @return the next uid 051 */ 052 public static int getNextUid() { 053 return idGenerator.incrementAndGet(); 054 } 055 056 /** 057 * Retrieve a WaveType according to its unique action name. 058 * 059 * Be careful it could return null if the {@link WaveType} has not been initialized yet. 060 * 061 * @param action the unique action name used to register the WaveType 062 * 063 * @return the WaveType found into registry or null 064 */ 065 public static WaveType getWaveType(final String action) { 066 067 WaveType waveType = null; 068 if (waveTypeMap.containsKey(action)) { 069 waveType = waveTypeMap.get(action); 070 } 071 return waveType; 072 } 073 074 /** 075 * Store a {@link WaveType} by its action name. 076 * 077 * @param action the action 078 * @param waveType the wave type 079 */ 080 public static void store(final String action, final WaveType waveType) { 081 082 // it uses action without any prefix 083 if (!waveTypeMap.containsKey(action)) { 084 waveTypeMap.put(action, waveType); 085 } 086 087 } 088 089}