下面列出了javax.annotation.RegEx#com.helger.commons.collection.impl.ICommonsList 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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 ());
}
/**
* 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;
});
}
@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));
}
}
@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);
}
/**
* 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;
}
/**
* 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));
}
}
/**
* 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;
}
@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));
}
}
/**
* 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 ≥ 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 ();
}
/**
* @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);
}
/**
* @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 ();
}
}
@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 ());
}
/**
* 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 ();
});
}
/**
* 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;
}
/**
* @return A copy of all contained media queries. Never <code>null</code>.
* Maybe empty.
*/
@Nonnull
@ReturnsMutableCopy
public ICommonsList <CSSMediaQuery> getAllMediaQueries ()
{
return m_aMediaQueries.getClone ();
}
/**
* @return A list of {@link String}, {@link PSDir}, {@link PSEmph} and
* {@link PSSpan} elements.
*/
@Nonnull
@ReturnsMutableCopy
public ICommonsList <Object> getAllContentElements ()
{
return m_aContent.getClone ();
}
/**
* Test issue 2859181 where an escaped character before a character that did
* not need escaping was causing the parse to fail.
*
* @throws IOException
* never
*/
@Test
public void testIssue2859181 () throws IOException
{
m_aParser = new CSVParser ().setSeparatorChar (';');
final ICommonsList <String> aNextLine = m_aParser.parseLine ("field1;\\=field2;\"\"\"field3\"\"\""); // field1;\=field2;"""field3"""
assertEquals (3, aNextLine.size ());
assertEquals ("field1", aNextLine.get (0));
assertEquals ("=field2", aNextLine.get (1));
assertEquals ("\"field3\"", aNextLine.get (2));
}
public static <DATATYPE> void iterateAllCombinations (@Nonnull final ICommonsList <DATATYPE> aElements,
final boolean bAllowEmpty,
@Nonnull final Consumer <? super ICommonsList <DATATYPE>> aCallback)
{
new CombinationGeneratorFlexible <DATATYPE> (aElements.size (), bAllowEmpty).iterateAllCombinations (aElements,
aCallback);
}
@Nonnull
@ReturnsMutableCopy
public static ICommonsList <String> getUBLPETestDocuments (@Nonnull final EDianUBLDocumentType eType)
{
ICommonsList <String> aFiles;
switch (eType)
{
case APPLICATION_RESPONSE:
aFiles = new CommonsArrayList <> ();
break;
case ATTACHED_DOCUMENT:
aFiles = new CommonsArrayList <> ();
break;
case CREDIT_NOTE:
aFiles = new CommonsArrayList <> (PREFIX + "CreditNote.xml");
break;
case DEBIT_NOTE:
aFiles = new CommonsArrayList <> (PREFIX + "DebitNote.xml");
break;
case INVOICE:
aFiles = new CommonsArrayList <> (PREFIX + "Combustible.xml",
PREFIX + "Consumidor Final.xml",
PREFIX + "EmisorAutoretenedor.xml",
PREFIX + "ExcluidosExentos.xml",
PREFIX + "Exportacion.xml",
PREFIX + "Generica.xml",
PREFIX + "GenericaPagoAnticipado.xml",
PREFIX + "Mandatos.xml",
PREFIX + "Servicios.xml");
break;
default:
throw new IllegalArgumentException ("No test files available for type " + eType);
}
return aFiles;
}
/**
* Get all mime types that are associated to the specified filename extension.
*
* @param sExtension
* The filename extension to search. May not be <code>null</code>.
* @return Never <code>null</code> but maybe empty set if no mime type is
* associated with the passed extension.
*/
@Nonnull
@ReturnsMutableCopy
public ICommonsOrderedSet <IMimeType> getAllMimeTypesForExtension (@Nonnull final String sExtension)
{
ValueEnforcer.notNull (sExtension, "Extension");
final ICommonsOrderedSet <IMimeType> ret = new CommonsLinkedHashSet <> ();
final ICommonsList <MimeTypeInfo> aInfos = getAllInfosOfExtension (sExtension);
if (aInfos != null)
for (final MimeTypeInfo aInfo : aInfos)
ret.addAll (aInfo.getAllMimeTypes ());
return ret;
}
public final void endWithoutAutoSave ()
{
final ICommonsList <IDAO> aDAOs = getAllContainedDAOs ();
m_aRWLock.writeLocked ( () -> {
for (final IDAO aDAO : aDAOs)
if (aDAO != null)
aDAO.endWithoutAutoSave ();
});
}
/**
* Read a predefined XML file that contains list items.
*
* @param aIS
* The input stream to read from. May not be <code>null</code>.
* Automatically closed no matter whether reading succeeded or not.
* @return <code>null</code> if reading fails - all list items otherwise.
*/
@Nullable
@ReturnsMutableCopy
public static ICommonsList <String> readList (@Nonnull @WillClose final InputStream aIS)
{
final ICommonsList <String> ret = new CommonsArrayList <> ();
if (readList (aIS, ret).isFailure ())
return null;
return ret;
}
/**
* @return All contained bound elements. It has the same amount of elements as
* the source assert/report.
*/
@Nonnull
@ReturnsMutableCopy
public final ICommonsList <PSXPathBoundElement> getAllBoundContentElements ()
{
return m_aBoundContent.getClone ();
}
@Nonnull
private static ICommonsList <SchematronTestFile> _readDI (@Nonnull final IReadableResource aRes)
{
if (false)
ClassPathHelper.getAllClassPathEntries ().forEach (x -> {
System.out.println (x);
if (new File (x).isDirectory ())
{
final FileSystemRecursiveIterator it = new FileSystemRecursiveIterator (new File (x));
it.forEach (y -> System.out.println (StringHelper.getRepeated (" ", it.getLevel ()) + y));
}
});
ValueEnforcer.notNull (aRes, "Resource");
ValueEnforcer.isTrue (aRes.exists (), () -> "Resource " + aRes + " does not exist!");
final ICommonsList <SchematronTestFile> ret = new CommonsArrayList <> ();
final IMicroDocument aDoc = MicroReader.readMicroXML (aRes);
if (aDoc == null)
throw new IllegalArgumentException ("Failed to open/parse " + aRes + " as XML");
String sLastParentDirBaseName = null;
for (final IMicroElement eItem : aDoc.getDocumentElement ().getAllChildElements ())
if (eItem.getTagName ().equals ("directory"))
sLastParentDirBaseName = eItem.getAttributeValue ("basename");
else
if (eItem.getTagName ().equals ("file"))
ret.add (new SchematronTestFile (sLastParentDirBaseName,
new ClassPathResource (eItem.getAttributeValue ("name")),
eItem.getAttributeValue ("basename")));
else
throw new IllegalArgumentException ("Cannot handle " + eItem);
return ret;
}
@Nonnull
@Nonempty
public static ICommonsList <IReadableResource> getAllValidSVRLFiles ()
{
return s_aSVRLs.getAllMapped (aFile -> !aFile.getFileBaseName ().startsWith ("invalid"),
SchematronTestFile::getResource);
}
@Test
public void testIsEmpty ()
{
assertTrue (isEmpty ((ICommonsList <?>) null));
assertTrue (isEmpty ((Map <?, ?>) null));
assertTrue (isEmpty (new CommonsVector <> ()));
assertTrue (isEmpty (new CommonsHashMap <> ()));
assertFalse (isEmpty (newList ("d", "c", "b", "a")));
assertTrue (isEmpty ((Iterable <?>) new NonBlockingStack <> ()));
}
@Test
public void testWhitespaceBeforeEscape () throws IOException
{
final ICommonsList <String> nextItem = m_aParser.parseLine ("\"this\", \"is\",\"a test\""); // "this",
// "is","a test"
assertEquals ("this", nextItem.get (0));
assertEquals ("is", nextItem.get (1));
assertEquals ("a test", nextItem.get (2));
}
/**
* Get all globs (=filename patterns) associated to the specified mime type
*
* @param aMimeType
* The mime type to search. May be <code>null</code>.
* @return Never <code>null</code> but empty set if no globs are present.
*/
@Nonnull
@ReturnsMutableCopy
public ICommonsOrderedSet <String> getAllGlobsOfMimeType (@Nullable final IMimeType aMimeType)
{
final ICommonsOrderedSet <String> ret = new CommonsLinkedHashSet <> ();
final ICommonsList <MimeTypeInfo> aInfos = getAllInfosOfMimeType (aMimeType);
if (aInfos != null)
for (final MimeTypeInfo aInfo : aInfos)
ret.addAll (aInfo.getAllGlobs ());
return ret;
}
@Test
public void testCorrectlyParserNullObject () throws IOException
{
final NonBlockingStringWriter aSW = new NonBlockingStringWriter ();
try (final CSVWriter aWriter = new CSVWriter (aSW))
{
aWriter.setQuoteChar ('\'');
aWriter.writeNext ((String []) null, false);
assertEquals (0, aSW.getAsString ().length ());
aWriter.writeNext ((ICommonsList <String>) null, false);
assertEquals (0, aSW.getAsString ().length ());
}
}
@Nullable
@ReturnsMutableObject
private Map.Entry <String, ICommonsList <String>> _getHeaderEntry (@Nullable final String sName)
{
if (StringHelper.hasText (sName))
for (final Map.Entry <String, ICommonsList <String>> aEntry : m_aHeaders.entrySet ())
if (aEntry.getKey ().equalsIgnoreCase (sName))
return aEntry;
return null;
}