com.helger.commons.collection.impl.ICommonsList Java Examples

The following examples show how to use com.helger.commons.collection.impl.ICommonsList. 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: CombinationGeneratorTest.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void testStringCombination2 ()
{
  final ICommonsList <String> aElements = new CommonsArrayList <> (A, B, B, C);
  final CombinationGenerator <String> x = new CombinationGenerator <> (aElements, 0);
  assertEquals (BigInteger.ONE, x.getTotalCombinations ());
  assertEquals (BigInteger.ONE, x.getCombinationsLeft ());

  final ICommonsList <ICommonsList <String>> aResultsWithDuplicates = new CommonsArrayList <> ();
  final ICommonsSet <ICommonsList <String>> aResultsWithoutDuplicates = new CommonsHashSet <> ();
  while (x.hasNext ())
  {
    final ICommonsList <String> aResult = x.next ();
    aResultsWithDuplicates.add (aResult);
    aResultsWithoutDuplicates.add (aResult);
  }
  assertEquals (1, aResultsWithDuplicates.size ());
  assertEquals (1, aResultsWithoutDuplicates.size ());
}
 
Example #2
Source File: CollectionHelperTest.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void testNewListCollection ()
{
  final ICommonsList <String> aSource = newList ("Hallo", "Welt", "from", "Vienna");

  ICommonsList <String> aList = newList (aSource);
  assertNotNull (aList);
  assertEquals (4, aList.size ());
  assertTrue (aList.contains ("Hallo"));
  assertTrue (aList.contains ("Welt"));
  assertTrue (aList.contains ("from"));
  assertTrue (aList.contains ("Vienna"));

  aList = newList (new CommonsArrayList <String> ());
  assertNotNull (aList);

  aList = newList ((ICommonsList <String>) null);
  assertNotNull (aList);
}
 
Example #3
Source File: AuthTokenRegistry.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
/**
 * Remove all tokens of the given subject
 *
 * @param aSubject
 *        The subject for which the tokens should be removed.
 * @return The number of removed tokens. Always &ge; 0.
 */
@Nonnegative
public static int removeAllTokensOfSubject (@Nonnull final IAuthSubject aSubject)
{
  ValueEnforcer.notNull (aSubject, "Subject");

  // get all token IDs matching a given subject
  // Note: required IAuthSubject to implement equals!
  final ICommonsList <String> aDelTokenIDs = new CommonsArrayList <> ();
  s_aRWLock.readLocked ( () -> {
    for (final Map.Entry <String, AuthToken> aEntry : s_aMap.entrySet ())
      if (aEntry.getValue ().getIdentification ().hasAuthSubject (aSubject))
        aDelTokenIDs.add (aEntry.getKey ());
  });

  for (final String sDelTokenID : aDelTokenIDs)
    removeToken (sDelTokenID);

  return aDelTokenIDs.size ();
}
 
Example #4
Source File: CSVReaderTest.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void testBug106ParseLineWithCarriageReturnNewLineStrictQuotes () throws IOException
{
  final StringBuilder aSB = new StringBuilder (CCSV.INITIAL_STRING_SIZE);

  // "a","123\r\n4567","aReader"
  aSB.append ("\"a\",\"123\r\n4567\",\"aReader\"").append ('\n');

  // public CSVReader(Reader reader, char separator, char quotechar, char
  // escape, int line, boolean strictQuotes,
  // boolean ignoreLeadingWhiteSpace, boolean keepCarriageReturn)
  try (final CSVReader aReader = new CSVReader (new NonBlockingStringReader (aSB.toString ()), true))
  {
    aReader.setStrictQuotes (true);

    final ICommonsList <String> nextLine = aReader.readNext ();
    assertEquals (3, nextLine.size ());

    assertEquals ("a", nextLine.get (0));
    assertEquals (1, nextLine.get (0).length ());

    assertEquals ("123\r\n4567", nextLine.get (1));
    assertEquals ("aReader", nextLine.get (2));
  }
}
 
Example #5
Source File: CSVReaderTest.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void testEscapedQuote () throws IOException
{

  final StringBuilder aSB = new StringBuilder ();

  // a,123"4",aReader
  aSB.append ("a,\"123\\\"4567\",aReader").append ('\n');

  try (final CSVReader aReader = new CSVReader (new NonBlockingStringReader (aSB.toString ())))
  {
    final ICommonsList <String> nextLine = aReader.readNext ();
    assertEquals (3, nextLine.size ());

    assertEquals ("123\"4567", nextLine.get (1));
  }
}
 
Example #6
Source File: VendorInfo.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
/**
 * @return These are the lines that should used for prefixing generated files.
 */
@Nonnull
@ReturnsMutableCopy
public static ICommonsList <String> getFileHeaderLines ()
{
  final int nYear = PDTFactory.getCurrentYear ();
  return new CommonsArrayList <> ("THIS FILE IS GENERATED - DO NOT EDIT",
                                  "",
                                  "Copyright",
                                  "",
                                  "Copyright (c) " + getVendorName () + " " + getInceptionYear () + " - " + nYear,
                                  getVendorURL (),
                                  "",
                                  "All Rights Reserved",
                                  "Use, duplication or disclosure restricted by " + getVendorName (),
                                  "",
                                  getVendorLocation () + ", " + getInceptionYear () + " - " + nYear);
}
 
Example #7
Source File: ScopeSPIManager.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
/**
 * @return All registered request scope SPI listeners. Never <code>null</code>
 *         but maybe empty.
 */
@Nonnull
@ReturnsMutableCopy
public ICommonsList <IRequestScopeSPI> getAllRequestScopeSPIs ()
{
  // Is called very often!
  m_aRWLock.readLock ().lock ();
  try
  {
    return m_aRequestSPIs.getClone ();
  }
  finally
  {
    m_aRWLock.readLock ().unlock ();
  }
}
 
Example #8
Source File: SVRLHelper.java    From ph-schematron with Apache License 2.0 6 votes vote down vote up
/**
 * Get a list of all successful reports in a given schematron output, with an
 * error level equally or more severe than the passed error level.
 *
 * @param aSchematronOutput
 *        The schematron output to be used. May be <code>null</code>.
 * @param aErrorLevel
 *        Minimum error level to be queried
 * @return A non-<code>null</code> list with all successful reports.
 */
@Nonnull
@ReturnsMutableCopy
public static ICommonsList <SVRLSuccessfulReport> getAllSuccessfulReportsMoreOrEqualSevereThan (@Nullable final SchematronOutputType aSchematronOutput,
                                                                                                @Nonnull final IErrorLevel aErrorLevel)
{
  final ICommonsList <SVRLSuccessfulReport> ret = new CommonsArrayList <> ();
  if (aSchematronOutput != null)
    for (final Object aObj : aSchematronOutput.getActivePatternAndFiredRuleAndFailedAssert ())
      if (aObj instanceof SuccessfulReport)
      {
        final SVRLSuccessfulReport aSR = new SVRLSuccessfulReport ((SuccessfulReport) aObj);
        if (aSR.getFlag ().isGE (aErrorLevel))
          ret.add (aSR);
      }
  return ret;
}
 
Example #9
Source File: CollectionHelperTest.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetDifference ()
{
  final ICommonsList <String> l1 = newList ("Hello", "Welt", "from", "Vienna");
  final ICommonsList <String> l2 = newList ("Welt", "from");

  // Result should be "Hello" and "Vienna"
  final Set <String> ret = getDifference (l1, l2);
  assertNotNull (ret);
  assertEquals (2, ret.size ());
  assertTrue (ret.contains ("Hello"));
  assertFalse (ret.contains ("Welt"));
  assertFalse (ret.contains ("from"));
  assertTrue (ret.contains ("Vienna"));

  assertEquals (4, getDifference (l1, new CommonsVector <String> ()).size ());
  assertEquals (4, getDifference (l1, null).size ());
  assertEquals (0, getDifference (new CommonsHashSet <String> (), l2).size ());
  assertEquals (0, getDifference (null, l2).size ());
}
 
Example #10
Source File: CSVReaderTest.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
/**
 * Same as testADoubleQuoteAsDataElement but I changed the quotechar to a
 * single quote. Also the middle field is empty.
 *
 * @throws IOException
 *         never
 */
@Test
public void testASingleQuoteAsDataElementWithEmptyField () throws IOException
{
  final StringBuilder aSB = new StringBuilder (CCSV.INITIAL_STRING_SIZE);

  // a,,aReader
  aSB.append ("a,'',aReader").append ('\n');

  try (final CSVReader aReader = new CSVReader (new NonBlockingStringReader (aSB.toString ())))
  {
    aReader.setQuoteChar ('\'');
    final ICommonsList <String> nextLine = aReader.readNext ();
    assertEquals (3, nextLine.size ());

    assertEquals ("a", nextLine.get (0));
    assertEquals (0, nextLine.get (1).length ());
    assertEquals ("", nextLine.get (1));
    assertEquals ("aReader", nextLine.get (2));
  }
}
 
Example #11
Source File: SVRLHelper.java    From ph-schematron with Apache License 2.0 6 votes vote down vote up
/**
 * Get a list of all failed assertions in a given schematron output, with an
 * error level equally or more severe than the passed error level.
 *
 * @param aSchematronOutput
 *        The schematron output to be used. May be <code>null</code>.
 * @param aErrorLevel
 *        Minimum error level to be queried
 * @return A non-<code>null</code> list with all failed assertions.
 */
@Nonnull
@ReturnsMutableCopy
public static ICommonsList <SVRLFailedAssert> getAllFailedAssertionsMoreOrEqualSevereThan (@Nullable final SchematronOutputType aSchematronOutput,
                                                                                           @Nonnull final IErrorLevel aErrorLevel)
{
  final ICommonsList <SVRLFailedAssert> ret = new CommonsArrayList <> ();
  if (aSchematronOutput != null)
    for (final Object aObj : aSchematronOutput.getActivePatternAndFiredRuleAndFailedAssert ())
      if (aObj instanceof FailedAssert)
      {
        final SVRLFailedAssert aFA = new SVRLFailedAssert ((FailedAssert) aObj);
        if (aFA.getFlag ().isGE (aErrorLevel))
          ret.add (aFA);
      }
  return ret;
}
 
Example #12
Source File: MimeTypeInfoManager.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
/**
 * Get all infos associated with the specified filename extension.
 *
 * @param sExtension
 *        The extension to search. May be <code>null</code> or empty.
 * @return <code>null</code> if the passed extension is <code>null</code> or
 *         if no such extension is registered.
 */
@Nullable
@ReturnsMutableCopy
public ICommonsList <MimeTypeInfo> getAllInfosOfExtension (@Nullable final String sExtension)
{
  // Extension may be empty!
  if (sExtension == null)
    return null;

  return m_aRWLock.readLockedGet ( () -> {
    ICommonsList <MimeTypeInfo> ret = m_aMapExt.get (sExtension);
    if (ret == null)
    {
      // Especially on Windows, sometimes file extensions like "JPG" can be
      // found. Therefore also test for the lowercase version of the
      // extension.
      ret = m_aMapExt.get (sExtension.toLowerCase (Locale.US));
    }
    // Create a copy if present
    return ret == null ? null : ret.getClone ();
  });
}
 
Example #13
Source File: TypeConverterRegistry.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
/**
 * Get the converter that can convert objects from aSrcClass to aDstClass
 * using the registered rules. The first match is returned.
 *
 * @param aSrcClass
 *        Source class. May not be <code>null</code>.
 * @param aDstClass
 *        Destination class. May not be <code>null</code>.
 * @return <code>null</code> if no such type converter exists, the converter
 *         object otherwise.
 */
@Nullable
ITypeConverter <?, ?> getRuleBasedConverter (@Nullable final Class <?> aSrcClass, @Nullable final Class <?> aDstClass)
{
  if (aSrcClass == null || aDstClass == null)
    return null;

  return m_aRWLock.readLockedGet ( () -> {
    // Check all rules in the correct order
    for (final Map.Entry <ITypeConverterRule.ESubType, ICommonsList <ITypeConverterRule <?, ?>>> aEntry : m_aRules.entrySet ())
      for (final ITypeConverterRule <?, ?> aRule : aEntry.getValue ())
        if (aRule.canConvert (aSrcClass, aDstClass))
          return aRule;

    return null;
  });
}
 
Example #14
Source File: SimpleURL.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Override
public String toString ()
{
  return new ToStringGenerator (null).append ("Path", m_sPath)
                                     .appendIf ("Params", m_aParams, ICommonsList::isNotEmpty)
                                     .appendIfNotNull ("Anchor", m_sAnchor)
                                     .getToString ();
}
 
Example #15
Source File: CollectionHelperTest.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetConcatenatedList_CollectionCollection ()
{
  final ICommonsList <String> a = newList ("a", "b");
  final ICommonsList <String> b = newList ("c", "d");
  assertTrue (getConcatenatedList ((Collection <String>) null, (Collection <String>) null).isEmpty ());
  assertEquals (a, getConcatenatedList (a, (Collection <String>) null));
  assertEquals (b, getConcatenatedList ((Collection <String>) null, b));
  assertEquals (newList ("a", "b", "c", "d"), getConcatenatedList (a, b));
}
 
Example #16
Source File: CombinationGeneratorFlexible.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
public static <DATATYPE> ICommonsSet <ICommonsList <DATATYPE>> getCombinations (@Nonnull final ICommonsList <DATATYPE> aElements,
                                                                                final boolean bAllowEmpty)
{
  return new CombinationGeneratorFlexible <DATATYPE> (aElements.size (), bAllowEmpty).getCombinations (aElements);
}
 
Example #17
Source File: ChildrenProviderElementWithName.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
public ICommonsList <? extends IMicroElement> getAllChildren (@Nullable final IMicroElement aCurrent)
{
  // Not an element?
  if (aCurrent == null)
    return new CommonsArrayList <> ();

  // Namespace URI defined?
  if (StringHelper.hasText (m_sNamespaceURI))
    return aCurrent.getAllChildElements (m_sNamespaceURI, m_sTagName);

  return aCurrent.getAllChildElements (m_sTagName);
}
 
Example #18
Source File: CSVParserTest.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void testParseQuotedStringWithCommas () throws IOException
{
  final ICommonsList <String> aNextLine = m_aParser.parseLine ("a,\"b,b,b\",c");
  assertEquals ("a", aNextLine.get (0));
  assertEquals ("b,b,b", aNextLine.get (1));
  assertEquals ("c", aNextLine.get (2));
  assertEquals (3, aNextLine.size ());
}
 
Example #19
Source File: CSVParserTest.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void testNotStrictQuoteSimple () throws IOException
{
  m_aParser = new CSVParser ().setStrictQuotes (false);
  final String testString = "\"a\",\"b\",\"c\"";

  final ICommonsList <String> aNextLine = m_aParser.parseLine (testString);
  assertEquals (3, aNextLine.size ());
  assertEquals ("a", aNextLine.get (0));
  assertEquals ("b", aNextLine.get (1));
  assertEquals ("c", aNextLine.get (2));
}
 
Example #20
Source File: XMLDebug.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nullable
@ReturnsMutableCopy
public static ICommonsList <String> getAllSupportedFeatures (@Nonnull final EXMLDOMFeatureVersion eFeatureVersion)
{
  final ICommonsList <String> ret = s_aSupportedFeatures.get (eFeatureVersion);
  return ret == null ? null : ret.getClone ();
}
 
Example #21
Source File: MainFindOccurrances.java    From ph-ubl with Apache License 2.0 5 votes vote down vote up
/**
 * Get all fields of the specified class and all its super classes. This
 * method uses a cache to avoid retrieving the data over and over again.
 *
 * @param aClass
 *        The class to get all fields from
 * @return Never <code>null</code> but maybe empty.
 */
@Nonnull
private static ICommonsList <Field> getAllFields (@Nonnull final Class <?> aClass)
{
  ICommonsList <Field> ret = s_aAllFields.get (aClass);
  if (ret == null)
  {
    ret = new CommonsArrayList<> ();
    _getAllFields (aClass, ret);
    s_aAllFields.put (aClass, ret);
  }
  return ret;
}
 
Example #22
Source File: GenericReflectionTest.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void testUncheckedCast ()
{
  final ICommonsList <String> l = new CommonsArrayList <> ("a", "b");
  final Object o = l;
  final ICommonsList <String> l2 = GenericReflection.uncheckedCast (o);
  assertSame (l, l2);
}
 
Example #23
Source File: XMLHelper.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
public static ICommonsList <Attr> getAllAttributesAsList (@Nullable final Element aSrcNode)
{
  final ICommonsList <Attr> ret = new CommonsArrayList <> ();
  NamedNodeMapIterator.createAttributeIterator (aSrcNode).forEach (x -> ret.add ((Attr) x));
  return ret;
}
 
Example #24
Source File: ChildrenProviderHasChildrenSorting.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Override
@Nullable
public ICommonsList <? extends CHILDTYPE> getAllChildren (@Nullable final CHILDTYPE aCurrent)
{
  // Get all children of the passed element
  final ICommonsCollection <? extends CHILDTYPE> ret = aCurrent == null ? null : aCurrent.getAllChildren ();

  // If there is anything to sort do it now
  return ret == null ? null : ret.getSorted (m_aComparator);
}
 
Example #25
Source File: CSVReader.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
/**
 * Reads the entire file into a list with each element being a list of
 * {@link String} of tokens.
 *
 * @return a list of list of String, with each inner list of {@link String}
 *         representing a line of the file.
 * @throws IOException
 *         if bad things happen during the read
 */
@Nonnull
@ReturnsMutableCopy
public ICommonsList <ICommonsList <String>> readAll () throws IOException
{
  final ICommonsList <ICommonsList <String>> ret = new CommonsArrayList <> ();
  while (m_bHasNext)
  {
    final ICommonsList <String> aNextLineAsTokens = readNext ();
    if (aNextLineAsTokens != null)
      ret.add (aNextLineAsTokens);
  }
  return ret;
}
 
Example #26
Source File: MimeTypeInfoManager.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
/**
 * Get the primary (=first) extension for the specified mime type.
 *
 * @param aMimeType
 *        The mime type to be searched. May be <code>null</code>.
 * @return <code>null</code> if the mime type has no file extension assigned
 */
@Nullable
public String getPrimaryExtensionOfMimeType (@Nullable final IMimeType aMimeType)
{
  final ICommonsList <MimeTypeInfo> aInfos = getAllInfosOfMimeType (aMimeType);
  if (aInfos != null)
    for (final MimeTypeInfo aInfo : aInfos)
    {
      // Not every info has an extension!
      final String ret = aInfo.getPrimaryExtension ();
      if (ret != null)
        return ret;
    }
  return null;
}
 
Example #27
Source File: CollectingCSSParseErrorHandler.java    From ph-css with Apache License 2.0 5 votes vote down vote up
/**
 * @return A copy of the list with all contained errors. Never
 *         <code>null</code> but maybe empty.
 * @see #getParseErrorCount()
 * @see #hasParseErrors()
 */
@Nonnull
@ReturnsMutableCopy
public ICommonsList <CSSParseError> getAllParseErrors ()
{
  return m_aRWLock.readLockedGet (m_aErrors::getClone);
}
 
Example #28
Source File: JAXBContextCacheKey.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
private JAXBContext _createFromClassesAndProperties (final boolean bSilentMode)
{
  final ICommonsList <Class <?>> aClasses = _getAllClasses ();

  // E.g. an internal class - try anyway!
  if (!bSilentMode)
    if (LOGGER.isInfoEnabled ())
      LOGGER.info ("Creating JAXB context for classes " +
                   StringHelper.getImplodedMapped (", ", aClasses, x -> '\'' + x.getName () + '\'') +
                   (m_aProperties.isEmpty () ? "" : " with properties " + m_aProperties.keySet ()));

  try
  {
    // Using the version with a ClassLoader would require an
    // ObjectFactory.class or an jaxb.index file in the same package!
    final Class <?> [] aClassArray = aClasses.toArray (ArrayHelper.EMPTY_CLASS_ARRAY);
    return JAXBContext.newInstance (aClassArray, m_aProperties);
  }
  catch (final JAXBException ex)
  {
    final String sMsg = "Failed to create JAXB context for classes " +
                        StringHelper.getImplodedMapped (", ", aClasses, x -> '\'' + x.getName () + '\'') +
                        (m_aProperties.isEmpty () ? "" : " with properties " + m_aProperties.keySet ());
    LOGGER.error (sMsg + ": " + ex.getMessage ());
    throw new IllegalArgumentException (sMsg, ex);
  }
}
 
Example #29
Source File: PSPhase.java    From ph-schematron with Apache License 2.0 5 votes vote down vote up
/**
 * @return A list of {@link PSActive}, {@link PSLet} and {@link PSP} elements.
 */
@Nonnull
@ReturnsMutableCopy
public ICommonsList <IPSElement> getAllContentElements ()
{
  // Remove includes
  return m_aContent.getAllMapped (x -> x instanceof IPSElement && !(x instanceof PSInclude), x -> (IPSElement) x);
}
 
Example #30
Source File: AbstractMapBasedWALDAO.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
@IsLocked (ELockType.READ)
public final ICommonsList <INTERFACETYPE> getAll ()
{
  // Use new CommonsArrayList to get the return type to NOT use "? extends
  // INTERFACETYPE"
  return m_aRWLock.readLockedGet ( () -> new CommonsArrayList <> (m_aMap.values ()));
}