com.helger.commons.state.EChange Java Examples

The following examples show how to use com.helger.commons.state.EChange. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Graph.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Nonnull
public EChange addNode (@Nonnull final IMutableGraphNode aNode)
{
  ValueEnforcer.notNull (aNode, "Node");

  if (!isChangingConnectedObjectsAllowed () && aNode.hasRelations ())
    throw new IllegalArgumentException ("The node to be added already has incoming and/or outgoing relations and this is not allowed!");

  final String sID = aNode.getID ();
  if (m_aNodes.containsKey (sID))
    return EChange.UNCHANGED;
  m_aNodes.put (sID, aNode);

  _invalidateCache ();
  return EChange.CHANGED;
}
 
Example #2
Source File: ICommonsMap.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Nonnull
default EChange removeIf (@Nonnull final Predicate <? super Map.Entry <? extends KEYTYPE, ? extends VALUETYPE>> aFilter)
{
  ValueEnforcer.notNull (aFilter, "Filter");
  EChange ret = EChange.UNCHANGED;
  final Iterator <Map.Entry <KEYTYPE, VALUETYPE>> it = entrySet ().iterator ();
  while (it.hasNext ())
  {
    if (aFilter.test (it.next ()))
    {
      it.remove ();
      ret = EChange.CHANGED;
    }
  }
  return ret;
}
 
Example #3
Source File: BasicTreeItemWithID.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Nonnull
public final EChange removeAllChildren ()
{
  if (m_aChildMap == null || m_aChildMap.isEmpty ())
    return EChange.UNCHANGED;

  // Remember all children
  final ICommonsList <ITEMTYPE> aAllChildren = m_aChildren.getClone ();

  // Remove all children
  m_aChildMap.clear ();
  m_aChildren.clear ();

  // Notify factory after removal
  for (final ITEMTYPE aChild : aAllChildren)
    _recursiveRemoveFromFactory (aChild);
  return EChange.CHANGED;
}
 
Example #4
Source File: FileHelper.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Nonnull
public static EChange ensureParentDirectoryIsPresent (@Nonnull final File aFile)
{
  ValueEnforcer.notNull (aFile, "File");

  // If the file has no parent, it is located in the root...
  final File aParent = aFile.getParentFile ();
  if (aParent == null || aParent.exists ())
  {
    if (aParent != null && !aParent.isDirectory ())
      if (LOGGER.isWarnEnabled ())
        LOGGER.warn ("Parent object specified is not a directory: '" + aParent + "'");
    return EChange.UNCHANGED;
  }

  // Now try to create the directory
  final FileIOError aError = FileOperations.createDirRecursive (aParent);
  if (aError.isFailure ())
    throw new IllegalStateException ("Failed to create parent of " + aFile.getAbsolutePath () + ": " + aError);

  // Check again if it exists, to be 100% sure :)
  if (!aParent.exists ())
    throw new IllegalStateException ("Parent of " + aFile.getAbsolutePath () + " is still not existing!");
  return EChange.CHANGED;
}
 
Example #5
Source File: DirectedGraph.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Nonnull
public EChange addNode (@Nonnull final IMutableDirectedGraphNode aNode)
{
  ValueEnforcer.notNull (aNode, "Node");

  if (!isChangingConnectedObjectsAllowed () && aNode.hasRelations ())
    throw new IllegalArgumentException ("The node to be added already has incoming and/or outgoing relations and this is not allowed!");

  final String sID = aNode.getID ();
  if (m_aNodes.containsKey (sID))
    return EChange.UNCHANGED;
  m_aNodes.put (sID, aNode);

  _invalidateCache ();
  return EChange.CHANGED;
}
 
Example #6
Source File: ICommonsCollection.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
/**
 * Add all elements of the passed enumeration to this collection.
 *
 * @param aEnum
 *        The enumeration to be iterated and the elements to be added. May be
 *        <code>null</code>.
 * @param aFilter
 *        The filter to be applied. May be <code>null</code>.
 * @return {@link EChange#CHANGED} if at least one element was added,
 *         {@link EChange#UNCHANGED}. Never <code>null</code>.
 * @since 8.5.2
 */
@Nonnull
default EChange addAll (@Nullable final Enumeration <? extends ELEMENTTYPE> aEnum,
                        @Nullable final Predicate <? super ELEMENTTYPE> aFilter)
{
  if (aFilter == null)
    return addAll (aEnum);

  EChange eChange = EChange.UNCHANGED;
  if (aEnum != null)
    while (aEnum.hasMoreElements ())
    {
      final ELEMENTTYPE aElement = aEnum.nextElement ();
      if (aFilter.test (aElement))
        eChange = eChange.or (add (aElement));
    }
  return eChange;
}
 
Example #7
Source File: GraphNode.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Nonnull
public EChange addRelation (@Nullable final IMutableGraphRelation aRelation)
{
  if (aRelation == null)
    return EChange.UNCHANGED;
  if (!aRelation.isRelatedTo (this))
    throw new IllegalArgumentException ("Relation is not suitable for this node!");

  final String sRelationID = aRelation.getID ();
  if (m_aRelations == null)
    m_aRelations = new CommonsLinkedHashMap <> ();
  else
    if (m_aRelations.containsKey (sRelationID))
      return EChange.UNCHANGED;

  m_aRelations.put (sRelationID, aRelation);
  return EChange.CHANGED;
}
 
Example #8
Source File: MutableLong.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
public EChange set (final long nValue)
{
  if (nValue == m_nValue)
    return EChange.UNCHANGED;
  m_nValue = nValue;
  onAfterChange ();
  return EChange.CHANGED;
}
 
Example #9
Source File: CountryCache.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
@VisibleForTesting
final EChange addCountry (@Nonnull final String sCountry)
{
  ValueEnforcer.notNull (sCountry, "Country");
  final String sValidCountry = LocaleHelper.getValidCountryCode (sCountry);
  ValueEnforcer.isTrue (sValidCountry != null, () -> "illegal country code '" + sCountry + "'");
  ValueEnforcer.isTrue (sCountry.equals (sValidCountry), () -> "invalid casing of '" + sCountry + "'");

  return m_aRWLock.writeLockedGet ( () -> m_aCountries.addObject (sValidCountry));
}
 
Example #10
Source File: AbstractMicroNode.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
/**
 * Remove all children from this node.
 *
 * @return {@link EChange#CHANGED} if at least one child was present, and was
 *         successfully removed, {@link EChange#UNCHANGED} otherwise.
 */
@OverrideOnDemand
@Nonnull
protected EChange onRemoveAllChildren ()
{
  throw new MicroException ("Cannot remove all children from this node: " + getClass ().getName ());
}
 
Example #11
Source File: Settings.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public EChange putIn (@Nonnull @Nonempty final String sName, @Nullable final Object aNewValue)
{
  // Additional check, that name may not be empty
  ValueEnforcer.notEmpty (sName, "Name");

  return super.putIn (sName, aNewValue);
}
 
Example #12
Source File: Graph.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
public EChange removeNode (@Nonnull final IMutableGraphNode aNode)
{
  ValueEnforcer.notNull (aNode, "Node");

  if (!isChangingConnectedObjectsAllowed () && aNode.hasRelations ())
    throw new IllegalArgumentException ("The node to be removed already has incoming and/or outgoing relations and this is not allowed!");

  if (m_aNodes.remove (aNode.getID ()) == null)
    return EChange.UNCHANGED;

  _invalidateCache ();
  return EChange.CHANGED;
}
 
Example #13
Source File: GenericJAXBMarshaller.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
/**
 * Set the charset to be used for writing JAXB objects.
 *
 * @param aCharset
 *        The charset to be used by default. May be <code>null</code>.
 * @return {@link EChange}
 * @since 8.5.3
 */
@Nonnull
public final EChange setCharset (@Nullable final Charset aCharset)
{
  if (EqualsHelper.equals (aCharset, m_aCharset))
    return EChange.UNCHANGED;
  m_aCharset = aCharset;
  return EChange.CHANGED;
}
 
Example #14
Source File: MutableByte.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
public EChange set (final byte nValue)
{
  if (m_nValue == nValue)
    return EChange.UNCHANGED;
  m_nValue = nValue;
  onAfterChange ();
  return EChange.CHANGED;
}
 
Example #15
Source File: ICommonsCollection.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
/**
 * Add all elements of the passed iterable to this collection.
 *
 * @param aElements
 *        The elements to be added. May be <code>null</code>.
 * @param aFilter
 *        The filter to be applied. May be <code>null</code>.
 * @return {@link EChange#CHANGED} if at least one element was added,
 *         {@link EChange#UNCHANGED}. Never <code>null</code>.
 * @since 8.5.2
 */
@Nonnull
default EChange addAll (@Nullable final Iterable <? extends ELEMENTTYPE> aElements,
                        @Nullable final Predicate <? super ELEMENTTYPE> aFilter)
{
  if (aFilter == null)
    return addAll (aElements);

  EChange eChange = EChange.UNCHANGED;
  if (aElements != null)
    for (final ELEMENTTYPE aElement : aElements)
      if (aFilter.test (aElement))
        eChange = eChange.or (add (aElement));
  return eChange;
}
 
Example #16
Source File: Pair.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
public EChange setSecond (@Nullable final DATA2TYPE aSecond)
{
  if (EqualsHelper.equals (aSecond, m_aSecond))
    return EChange.UNCHANGED;
  m_aSecond = aSecond;
  return EChange.CHANGED;
}
 
Example #17
Source File: HttpHeaderMap.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
private EChange _addHeader (@Nonnull @Nonempty final String sName, @Nonnull final String sValue)
{
  ValueEnforcer.notEmpty (sName, "Name");
  ValueEnforcer.notNull (sValue, "Value");

  if (LOGGER.isDebugEnabled ())
    LOGGER.debug ("Adding HTTP header: '" + sName + "' = '" + sValue + "'");
  return _getOrCreateHeaderList (sName).addObject (sValue);
}
 
Example #18
Source File: SAXReaderDefaultSettings.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static EChange removePropertyValue (@Nullable final EXMLParserProperty eProperty)
{
  if (eProperty == null)
    return EChange.UNCHANGED;

  return s_aRWLock.writeLockedGet ( () -> s_aDefaultProperties.removeObject (eProperty));
}
 
Example #19
Source File: AbstractMapBasedMultilingualText.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
public final EChange assignFrom (@Nonnull final IMultilingualText aMLT)
{
  ValueEnforcer.notNull (aMLT, "MLT");

  if (texts ().equals (aMLT.texts ()) || _beforeChange ().isBreak ())
    return EChange.UNCHANGED;

  // Remove all existing texts and assign the new ones
  texts ().clear ();
  for (final Map.Entry <Locale, String> aEntry : aMLT.texts ().entrySet ())
    internalAddText (aEntry);
  _afterChange ();
  return EChange.CHANGED;
}
 
Example #20
Source File: MapBasedXPathFunctionResolver.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
/**
 * Add a new function.
 *
 * @param aName
 *        The qualified name of the function
 * @param nArity
 *        The number of parameters of the function
 * @param aFunction
 *        The function to be used. May not be <code>null</code>.
 * @return {@link EChange}
 */
@Nonnull
public EChange addUniqueFunction (@Nonnull final QName aName,
                                  @Nonnegative final int nArity,
                                  @Nonnull final XPathFunction aFunction)
{
  ValueEnforcer.notNull (aFunction, "Function");

  final XPathFunctionKey aFunctionKey = new XPathFunctionKey (aName, nArity);
  if (m_aMap.containsKey (aFunctionKey))
    return EChange.UNCHANGED;
  m_aMap.put (aFunctionKey, aFunction);
  return EChange.CHANGED;
}
 
Example #21
Source File: MapBasedNamespaceContext.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
public EChange clear ()
{
  if (m_aPrefix2NS.isEmpty ())
    return EChange.UNCHANGED;

  m_aPrefix2NS.clear ();
  m_aNS2Prefix.clear ();
  m_sDefaultNamespaceURI = null;
  return EChange.CHANGED;
}
 
Example #22
Source File: Graph.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
private IMutableGraphRelation _connect (@Nonnull final IMutableGraphRelation aRelation)
{
  EChange eChange = EChange.UNCHANGED;
  for (final IMutableGraphNode aNode : aRelation.getAllConnectedNodes ())
    eChange = eChange.or (aNode.addRelation (aRelation));
  if (eChange.isChanged ())
    _invalidateCache ();
  return aRelation;
}
 
Example #23
Source File: Graph.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
public EChange removeAll ()
{
  if (m_aNodes.removeAll ().isUnchanged ())
    return EChange.UNCHANGED;

  _invalidateCache ();
  return EChange.CHANGED;
}
 
Example #24
Source File: MapBasedNamespaceContextTest.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void testCloneIssue17 ()
{
  final MapBasedNamespaceContext aNSCtx = new MapBasedNamespaceContext ();
  aNSCtx.addMapping ("p1", "urn:example1");
  aNSCtx.addMapping ("p2", "urn:example2");
  final MapBasedNamespaceContext aNSCtx2 = aNSCtx.getClone ();
  // Remove from original
  assertSame (EChange.CHANGED, aNSCtx.removeMapping ("p1"));
  assertSame (EChange.UNCHANGED, aNSCtx.removeMapping ("p1"));
  // Remove from clone
  assertSame (EChange.CHANGED, aNSCtx2.removeMapping ("p1"));
  assertSame (EChange.UNCHANGED, aNSCtx2.removeMapping ("p1"));
}
 
Example #25
Source File: ClassHierarchyCache.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
/**
 * It's important to clear the cache upon application shutdown, because for
 * web applications, keeping a cache of classes may prevent the web
 * application from unloading
 *
 * @return {@link EChange}
 */
@Nonnull
public static EChange clearCache ()
{
  final EChange ret = s_aRWLock.writeLockedGet (s_aClassHierarchy::removeAll);
  if (ret.isUnchanged ())
    return EChange.UNCHANGED;

  if (LOGGER.isDebugEnabled ())
    LOGGER.debug ("Cache was cleared: " + ClassHierarchyCache.class.getName ());
  return EChange.CHANGED;
}
 
Example #26
Source File: MapBasedXPathVariableResolver.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
/**
 * Remove the variable with the specified name.
 *
 * @param sName
 *        The name to be removed. May be <code>null</code>.
 * @return {@link EChange}
 */
@Nonnull
public EChange removeVariable (@Nullable final String sName)
{
  if (sName == null)
    return EChange.UNCHANGED;
  return m_aMap.removeObject (sName);
}
 
Example #27
Source File: IMutableDirectedGraphNode.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
default EChange removeAllRelations ()
{
  EChange ret = EChange.UNCHANGED;
  ret = ret.or (removeAllIncomingRelations ());
  ret = ret.or (removeAllOutgoingRelations ());
  return ret;
}
 
Example #28
Source File: GlobalIDFactory.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static EChange setIntIDFactory (@Nullable final IIntIDFactory aFactory)
{
  return s_aRWLock.writeLockedGet ( () -> {
    if (EqualsHelper.equals (s_aIntIDFactory, aFactory))
      return EChange.UNCHANGED;
    if (LOGGER.isDebugEnabled ())
      LOGGER.debug ("Setting in-memory int ID factory " + aFactory);
    s_aIntIDFactory = aFactory;
    return EChange.CHANGED;
  });
}
 
Example #29
Source File: MicroAttribute.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
public EChange setAttributeValue (@Nonnull final String sAttributeValue)
{
  ValueEnforcer.notNull (sAttributeValue, "AttributeValue");
  if (sAttributeValue.equals (m_sAttributeValue))
    return EChange.UNCHANGED;
  m_sAttributeValue = sAttributeValue;
  return EChange.CHANGED;
}
 
Example #30
Source File: SAXReaderSettings.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
public final EChange removeFeature (@Nullable final EXMLParserFeature eFeature)
{
  if (eFeature == null)
    return EChange.UNCHANGED;
  return m_aFeatures.removeObject (eFeature);
}