Java Code Examples for com.helger.commons.ValueEnforcer#isFalse()

The following examples show how to use com.helger.commons.ValueEnforcer#isFalse() . 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: XMLHelper.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
/**
 * Get the full qualified attribute name to use for the given namespace
 * prefix. The result will e.g. be <code>xmlns</code> or
 * <code>{http://www.w3.org/2000/xmlns/}xmlns:foo</code>.
 *
 * @param sNSPrefix
 *        The namespace prefix to build the attribute name from. May be
 *        <code>null</code> or empty.
 * @return If the namespace prefix is empty (if it equals
 *         {@link XMLConstants#DEFAULT_NS_PREFIX} or <code>null</code>) than
 *         "xmlns" is returned, else "xmlns:<i>prefix</i>" is returned.
 */
@Nonnull
public static QName getXMLNSAttrQName (@Nullable final String sNSPrefix)
{
  if (sNSPrefix != null)
    ValueEnforcer.isFalse (sNSPrefix.contains (CXML.XML_PREFIX_NAMESPACE_SEP_STR),
                           () -> "prefix is invalid: " + sNSPrefix);

  if (sNSPrefix == null || sNSPrefix.equals (XMLConstants.DEFAULT_NS_PREFIX))
  {
    // Default (empty) namespace prefix
    return new QName (XMLConstants.XMLNS_ATTRIBUTE_NS_URI, XMLConstants.XMLNS_ATTRIBUTE);
  }
  // Named XML namespace prefix
  return new QName (XMLConstants.XMLNS_ATTRIBUTE_NS_URI, sNSPrefix, XMLConstants.XMLNS_ATTRIBUTE);
}
 
Example 2
Source File: StringHelper.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
/**
 * Just calls <code>replaceAll</code> as long as there are still replacements
 * found
 *
 * @param sInputString
 *        The input string where the text should be replace. If this parameter
 *        is <code>null</code> or empty, no replacement is done.
 * @param sSearchText
 *        The string to be replaced. May neither be <code>null</code> nor
 *        empty.
 * @param sReplacementText
 *        The string with the replacement. May not be <code>null</code> but
 *        may be empty.
 * @return The input string as is, if the input string is empty or if the
 *         string to be replaced is not contained.
 */
@Nullable
public static String replaceAllRepeatedly (@Nullable final String sInputString,
                                           @Nonnull final String sSearchText,
                                           @Nonnull final String sReplacementText)
{
  ValueEnforcer.notEmpty (sSearchText, "SearchText");
  ValueEnforcer.notNull (sReplacementText, "ReplacementText");
  ValueEnforcer.isFalse (sReplacementText.contains (sSearchText),
                         "Loop detection: replacementText must not contain searchText");

  // Is input string empty?
  if (hasNoText (sInputString))
    return sInputString;

  String sRet = sInputString;
  String sLastLiteral;
  do
  {
    sLastLiteral = sRet;
    sRet = replaceAll (sRet, sSearchText, sReplacementText);
  } while (!sLastLiteral.equals (sRet));
  return sRet;
}
 
Example 3
Source File: SerializationConverterRegistry.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
public <T> void registerSerializationConverter (@Nonnull final Class <T> aClass,
                                                @Nonnull final ISerializationConverter <T> aConverter)
{
  ValueEnforcer.notNull (aClass, "Class");
  ValueEnforcer.notNull (aConverter, "Converter");
  ValueEnforcer.isFalse (Serializable.class.isAssignableFrom (aClass),
                         () -> "The provided " + aClass.toString () + " is already implementing Serializable!");

  m_aRWLock.writeLocked ( () -> {
    // The main class should not already be registered
    if (m_aMap.containsKey (aClass))
      throw new IllegalArgumentException ("A micro type converter for class " + aClass + " is already registered!");

    // Automatically register the class, and all parent classes/interfaces
    for (final WeakReference <Class <?>> aCurWRSrcClass : ClassHierarchyCache.getClassHierarchyIterator (aClass))
    {
      final Class <?> aCurSrcClass = aCurWRSrcClass.get ();
      if (aCurSrcClass != null)
        if (!m_aMap.containsKey (aCurSrcClass))
        {
          m_aMap.put (aCurSrcClass, aConverter);
          if (LOGGER.isDebugEnabled ())
            LOGGER.debug ("Registered serialization converter for '" + aCurSrcClass.toString () + "'");
        }
    }
  });
}
 
Example 4
Source File: AuthIdentificationResult.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
/**
 * Constructor.
 *
 * @param aAuthToken
 *        The auth token. May not be <code>null</code> in case of success.
 *        Must be <code>null</code> in case of failure.
 * @param aCredentialValidationFailure
 *        The validation failure. May not be <code>null</code> in case of
 *        failure. Must be <code>null</code> in case of success.
 */
protected AuthIdentificationResult (@Nullable final IAuthToken aAuthToken,
                                    @Nullable final ICredentialValidationResult aCredentialValidationFailure)
{
  ValueEnforcer.isFalse (aAuthToken == null && aCredentialValidationFailure == null, "One parameter must be set");
  ValueEnforcer.isFalse (aAuthToken != null && aCredentialValidationFailure != null, "Only one parameter may be set");
  if (aCredentialValidationFailure != null && aCredentialValidationFailure.isSuccess ())
    throw new IllegalStateException ("Don't call this method for successfuly credential validation!");
  m_aAuthToken = aAuthToken;
  m_aCredentialValidationFailure = aCredentialValidationFailure;
}
 
Example 5
Source File: TypeConverterRegistry.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
/**
 * Register a default type converter.
 *
 * @param aSrcClass
 *        A non-<code>null</code> source class to convert from. Must be an
 *        instancable class.
 * @param aDstClass
 *        A non-<code>null</code> destination class to convert to. Must be an
 *        instancable class. May not equal the source class.
 * @param aConverter
 *        The convert to use. May not be <code>null</code>.
 */
private void _registerTypeConverter (@Nonnull final Class <?> aSrcClass,
                                     @Nonnull final Class <?> aDstClass,
                                     @Nonnull final ITypeConverter <?, ?> aConverter)
{
  ValueEnforcer.notNull (aSrcClass, "SrcClass");
  ValueEnforcer.isTrue (ClassHelper.isPublic (aSrcClass), () -> "Source " + aSrcClass + " is no public class!");
  ValueEnforcer.notNull (aDstClass, "DstClass");
  ValueEnforcer.isTrue (ClassHelper.isPublic (aDstClass), () -> "Destination " + aDstClass + " is no public class!");
  ValueEnforcer.isFalse (aSrcClass.equals (aDstClass),
                         "Source and destination class are equal and therefore no converter is required.");
  ValueEnforcer.notNull (aConverter, "Converter");
  ValueEnforcer.isFalse (aConverter instanceof ITypeConverterRule,
                         "Type converter rules must be registered via registerTypeConverterRule");
  if (ClassHelper.areConvertibleClasses (aSrcClass, aDstClass))
    if (LOGGER.isWarnEnabled ())
      LOGGER.warn ("No type converter needed between " +
                   aSrcClass +
                   " and " +
                   aDstClass +
                   " because types are convertible!");

  // The main class should not already be registered
  final Map <Class <?>, ITypeConverter <?, ?>> aSrcMap = _getOrCreateConverterMap (aSrcClass);
  if (aSrcMap.containsKey (aDstClass))
    throw new IllegalArgumentException ("A mapping from " + aSrcClass + " to " + aDstClass + " is already defined!");

  m_aRWLock.writeLocked ( () -> {
    // Automatically register the destination class, and all parent
    // classes/interfaces
    for (final WeakReference <Class <?>> aCurWRDstClass : ClassHierarchyCache.getClassHierarchyIterator (aDstClass))
    {
      final Class <?> aCurDstClass = aCurWRDstClass.get ();
      if (aCurDstClass != null)
        if (!aSrcMap.containsKey (aCurDstClass))
        {
          if (aSrcMap.put (aCurDstClass, aConverter) != null)
          {
            if (LOGGER.isWarnEnabled ())
              LOGGER.warn ("Overwriting converter from " + aSrcClass + " to " + aCurDstClass);
          }
          else
          {
            if (LOGGER.isTraceEnabled ())
              LOGGER.trace ("Registered type converter from '" +
                            aSrcClass.toString () +
                            "' to '" +
                            aCurDstClass.toString () +
                            "'");
          }
        }
    }
  });
}
 
Example 6
Source File: CSSSourceLocation.java    From ph-css with Apache License 2.0 3 votes vote down vote up
/**
 * Constructor.
 *
 * @param aFirstTokenArea
 *        Area of the first token. May be <code>null</code> if the last token
 *        area is not <code>null</code>.
 * @param aLastTokenArea
 *        Area of the last token. May be <code>null</code> if the first token
 *        area is not <code>null</code>.
 * @throws IllegalArgumentException
 *         if both areas are <code>null</code>.
 */
public CSSSourceLocation (@Nullable final CSSSourceArea aFirstTokenArea, @Nullable final CSSSourceArea aLastTokenArea)
{
  ValueEnforcer.isFalse (aFirstTokenArea == null && aLastTokenArea == null, "At least one of the areas must be set!");
  m_aFirstTokenArea = aFirstTokenArea;
  m_aLastTokenArea = aLastTokenArea;
}