Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2014 Philip Helger (www.helger.com) philip[at]helger[dot]com 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 com.helger.webbasics.state;
 
 import java.util.Map;
 
 
 
 
Per session registry for UI control state data.

Author(s):
Philip Helger
 
 {
  
ObjectType
 
   public static final ObjectType OT_HCNODE = new ObjectType ("hcnode");
   private static final Logger s_aLogger = LoggerFactory.getLogger (UIStateRegistry.class);
 
   private final ReadWriteLock m_aRWLock = new ReentrantReadWriteLock ();
   private final Map <ObjectTypeMap <StringIHasUIState>> m_aMap = new HashMap <ObjectTypeMap <StringIHasUIState>> ();
 
   public UIStateRegistry ()
   {}
 
   @Nonnull
   public static UIStateRegistry getCurrent ()
   {
     return getSessionSingleton (UIStateRegistry.class);
   }

  
Get the state with the passed ID for the current session. In case the state is a UIStateWrapper instance, it is returned as is.

Parameters:
aOT Object type
sStateID The state ID to be searched
Returns:
the IHasUIState for the specified control ID, if already registered or null
 
   @Nullable
   public IHasUIState getState (@Nullable final ObjectType aOT, @Nullable final String sStateID)
   {
     if (aOT == null)
       return null;
     if (StringHelper.hasNoText (sStateID))
       return null;
 
     .readLock ().lock ();
     try
     {
       IHasUIState ret = null;
 
       // Get mapping for requested ObjectType
      final Map <StringIHasUIStateaMap = .get (aOT);
      if (aMap != null)
      {
        // Lookup control ID for this object type
        ret = aMap.get (sStateID);
        if (ret == null)
        {
          // Try regular expressions (required for auto suggests in ebiz)
          for (final Map.Entry <StringIHasUIStateaEntry : aMap.entrySet ())
            if (RegExHelper.stringMatchesPattern (aEntry.getKey (), sStateID))
            {
              ret = aEntry.getValue ();
              break;
            }
        }
      }
      return ret;
    }
    finally
    {
      .readLock ().unlock ();
    }
  }

  
Get the state object in the specified type. If the saved state is a UIStateWrapper instance, the contained value is returned!

Parameters:
aOT The ObjectType to be resolved. May be null.
sStateID The state ID to be resolved.
Returns:
null if no such object was found.
  public <T> T getCastedState (@Nullable final ObjectType aOT, @Nullable final String sStateID)
  {
    final IHasUIState aObject = getState (aOTsStateID);
    if (aObject == null)
      return null;
    // Special handling for UI state wrapper to retrieve the contained object
    if (aObject instanceof UIStateWrapper <?>)
      return ((UIStateWrapper <?>) aObject).<T> getCastedObject ();
    // Regular cast
    return GenericReflection.<IHasUIState, T> uncheckedCast (aObject);
  }

  
Registers a new control for the passed tree ID

Parameters:
sStateID the ID of the state in register. May neither be null nor empty.
aNewState The state to set. May not be null.
Returns:
com.helger.commons.state.EChange.CHANGED if the control was registered
com.helger.commons.state.EChange.UNCHANGED if an illegal argument was passed or a control has already been registered for that ID
  @SuppressFBWarnings ("RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE")
  public EChange registerState (@Nonnull @Nonempty final String sStateID, @Nonnull final IHasUIState aNewState)
  {
    ValueEnforcer.notEmpty (sStateID"StateID");
    ValueEnforcer.notNull (aNewState"NewState");
    final ObjectType aOT = aNewState.getTypeID ();
    if (aOT == null)
      throw new IllegalStateException ("Object has no typeID: " + aNewState);
    .writeLock ().lock ();
    try
    {
      Map <StringIHasUIStateaMap = .get (aOT);
      if (aMap == null)
      {
        aMap = new HashMap <StringIHasUIState> ();
        .put (aOTaMap);
      }
      if (.isDebugEnabled () && aMap.containsKey (sStateID))
        .debug ("Overwriting " + aOT.getObjectTypeName () + " with ID " + sStateID + " with new object");
      aMap.put (sStateIDaNewState);
      return .;
    }
    finally
    {
      .writeLock ().unlock ();
    }
  }

  
Register a state for the passed HC element, using the internal ID of the element.

Parameters:
aNewElement The element to be added to the registry. May not be null.
Returns:
Never null.
  public EChange registerState (@Nonnull final IHCElement <?> aNewElement)
  {
    ValueEnforcer.notNull (aNewElement"NewElement");
    return registerState (aNewElement.ensureID ().getID (), aNewElement);
  }
  public EChange registerState (@Nonnull @Nonempty final String sStateID, @Nonnull final IHCNode aNewNode)
  {
    ValueEnforcer.notNull (aNewNode"NewNode");
    return registerState (sStateID, UIStateWrapper.create (aNewNode));
  }
  public EChange removeState (@Nullable final ObjectType aObjectType, @Nonnull @Nonempty final String sStateID)
  {
    ValueEnforcer.notEmpty (sStateID"StateID");
    .writeLock ().lock ();
    try
    {
      final Map <StringIHasUIStateaMap = .get (aObjectType);
      if (aMap == null)
        return .;
      return EChange.valueOf (aMap.remove (sStateID) != null);
    }
    finally
    {
      .writeLock ().unlock ();
    }
  }
  public boolean equals (final Object o)
  {
    if (o == this)
      return true;
    if (!(o instanceof UIStateRegistry))
      return false;
    final UIStateRegistry rhs = (UIStateRegistryo;
    return .equals (rhs.m_aMap);
  }
  public int hashCode ()
  {
    return new HashCodeGenerator (this).append ().getHashCode ();
  }
  public String toString ()
  {
    return ToStringGenerator.getDerived (super.toString ()).append ("map").toString ();
  }
New to GrepCode? Check out our FAQ X