下面列出了javax.annotation.RegEx#com.helger.commons.ValueEnforcer 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Nonnull
public EChange removeNodeAndAllRelations (@Nonnull final IMutableGraphNode aNode)
{
ValueEnforcer.notNull (aNode, "Node");
if (!m_aNodes.containsKey (aNode.getID ()))
return EChange.UNCHANGED;
// Remove all affected relations from all nodes
for (final IMutableGraphRelation aRelation : aNode.getAllRelations ())
for (final IMutableGraphNode aNode2 : aRelation.getAllConnectedNodes ())
aNode2.removeRelation (aRelation);
// Remove the node itself
if (removeNode (aNode).isUnchanged ())
throw new IllegalStateException ("Inconsistency removing node and all relations");
return EChange.CHANGED;
}
/**
* Execute a callback with autosave being disabled. Must be called outside a
* writeLock, as this method locks itself!
*
* @param aCallable
* The callback to be executed
* @return The result of the callback. May be <code>null</code>.
* @throws EXTYPE
* In case of an error
* @param <RETURNTYPE>
* Return type of the callable
* @param <EXTYPE>
* Exception type that may be thrown
*/
@Nullable
default <RETURNTYPE, EXTYPE extends Exception> RETURNTYPE performWithoutAutoSaveThrowing (@Nonnull final IThrowingSupplier <RETURNTYPE, EXTYPE> aCallable) throws EXTYPE
{
ValueEnforcer.notNull (aCallable, "Callable");
beginWithoutAutoSave ();
try
{
// Main call of callable
return aCallable.get ();
}
finally
{
endWithoutAutoSave ();
}
}
@Nullable
public static RandomAccessFile getRandomAccessFile (@Nonnull final File aFile,
@Nonnull final ERandomAccessFileMode eMode)
{
ValueEnforcer.notNull (aFile, "File");
ValueEnforcer.notNull (eMode, "Mode");
try
{
return new RandomAccessFile (aFile, eMode.getMode ());
}
catch (final FileNotFoundException ex)
{
return null;
}
}
/**
* Reads as much as possible into the destination buffer.
*
* @param aDestByteBuffer
* The destination byte buffer to use. May not be <code>null</code>.
* @return The number of bytes read. Always ≥ 0.
*/
@Nonnegative
public long read (@Nonnull final ByteBuffer aDestByteBuffer)
{
ValueEnforcer.notNull (aDestByteBuffer, "DestByteBuffer");
_checkClosed ();
long nBytesRead = 0;
while (m_nBufferIndex < m_aBuffers.length)
{
final ByteBuffer aByteBuffer = m_aBuffers[m_nBufferIndex];
if (aByteBuffer.hasRemaining ())
nBytesRead += ByteBufferHelper.transfer (aByteBuffer, aDestByteBuffer, false);
if (!aDestByteBuffer.hasRemaining ())
break;
// Try next ByteBuffer
++m_nBufferIndex;
}
return nBytesRead;
}
/**
* Create a new XPath expression for evaluation.
*
* @param aXPath
* The pre-created XPath object. May not be <code>null</code>.
* @param sXPath
* The main XPath string to be evaluated
* @return The {@link XPathExpression} object to be used.
* @throws IllegalArgumentException
* if the XPath cannot be compiled
*/
@Nonnull
public static XPathExpression createNewXPathExpression (@Nonnull final XPath aXPath,
@Nonnull @Nonempty final String sXPath)
{
ValueEnforcer.notNull (aXPath, "XPath");
ValueEnforcer.notNull (sXPath, "XPathExpression");
try
{
return aXPath.compile (sXPath);
}
catch (final XPathExpressionException ex)
{
throw new IllegalArgumentException ("Failed to compile XPath expression '" + sXPath + "'", ex);
}
}
/**
* Add all passed elements matching the provided filter after performing a
* mapping using the provided function.
*
* @param aElements
* The elements to be added after mapping. May be <code>null</code>.
* @param aMapper
* The mapping function to be executed for all provided elements. May
* not be <code>null</code>.
* @param aFilter
* The filter to be applied on the mapped element. May be
* <code>null</code>.
* @return {@link EChange#CHANGED} if at least one element was added,
* {@link EChange#UNCHANGED}. Never <code>null</code>.
* @param <SRCTYPE>
* The source type to be mapped from
* @since 8.5.2
*/
@Nonnull
default <SRCTYPE> EChange addAllMapped (@Nullable final SRCTYPE [] aElements,
@Nonnull final Function <? super SRCTYPE, ? extends ELEMENTTYPE> aMapper,
@Nullable final Predicate <? super ELEMENTTYPE> aFilter)
{
ValueEnforcer.notNull (aMapper, "Mapper");
if (aFilter == null)
return addAllMapped (aElements, aMapper);
EChange eChange = EChange.UNCHANGED;
if (aElements != null)
for (final SRCTYPE aValue : aElements)
{
final ELEMENTTYPE aMapped = aMapper.apply (aValue);
if (aFilter.test (aMapped))
eChange = eChange.or (add (aMapped));
}
return eChange;
}
/**
* Resolve the given namespace URI to a prefix using the known namespaces of
* this stack.
*
* @param sNamespaceURI
* The namespace URI to resolve. May not be <code>null</code>. Pass
* in an empty string for an empty namespace URI!
* @return <code>null</code> if no namespace prefix is required.
*/
@Nullable
private String _getUsedPrefixOfNamespace (@Nonnull final String sNamespaceURI)
{
ValueEnforcer.notNull (sNamespaceURI, "NamespaceURI");
// find existing prefix (iterate current to root)
for (final NamespaceLevel aNSLevel : m_aStack)
{
final String sPrefix = aNSLevel.getPrefixOfNamespaceURI (sNamespaceURI);
if (sPrefix != null)
return sPrefix;
}
// no matching prefix found
return null;
}
/**
* Gets a sublist excerpt of the passed list.
*
* @param <ELEMENTTYPE>
* Type of elements in list
* @param aCont
* The backing list. May not be <code>null</code>.
* @param nStartIndex
* The start index to use. Needs to be ≥ 0.
* @param nSectionLength
* the length of the desired subset. If list is shorter than that,
* aIter will return a shorter section
* @return The specified section of the passed list, or a shorter list if
* nStartIndex + nSectionLength is an invalid index. Never
* <code>null</code>.
*/
@Nonnull
@ReturnsMutableCopy
public static <ELEMENTTYPE> CommonsArrayList <ELEMENTTYPE> getSubList (@Nullable final List <ELEMENTTYPE> aCont,
@Nonnegative final int nStartIndex,
@Nonnegative final int nSectionLength)
{
ValueEnforcer.isGE0 (nStartIndex, "StartIndex");
ValueEnforcer.isGE0 (nSectionLength, "SectionLength");
final int nSize = getSize (aCont);
if (nSize == 0 || nStartIndex >= nSize)
return newList (0);
int nEndIndex = nStartIndex + nSectionLength;
if (nEndIndex > nSize)
nEndIndex = nSize;
// Create a copy of the list because "subList" only returns a view of the
// original list!
return newList (aCont.subList (nStartIndex, nEndIndex));
}
public PreprocessorLookup (@Nonnull final PSSchema aSchema)
{
ValueEnforcer.notNull (aSchema, "Schema");
for (final PSPattern aPattern : aSchema.getAllPatterns ())
{
// Only handle abstract patterns
if (aPattern.isAbstract ())
m_aPatterns.put (aPattern.getID (), aPattern);
for (final PSRule aRule : aPattern.getAllRules ())
{
// Only handle abstract rules
if (aRule.isAbstract ())
m_aRules.put (aRule.getID (), aRule);
}
}
}
/**
* Create a new directory. The direct parent directory already needs to exist.
*
* @param aDir
* The directory to be created. May not be <code>null</code>.
* @return A non-<code>null</code> error code.
*/
@Nonnull
public static FileIOError createDir (@Nonnull final Path aDir)
{
ValueEnforcer.notNull (aDir, "Directory");
final Path aRealDir = _getUnifiedPath (aDir);
return FileOperations.createDir (aRealDir.toFile ());
// // Does the directory already exist?
// if (Files.exists (aRealDir))
// return EFileIOErrorCode.TARGET_ALREADY_EXISTS.getAsIOError
// (EFileIOOperation.CREATE_DIR, aRealDir);
//
// // Is the parent directory writable?
// final Path aParentDir = aRealDir.getParent ();
// if (aParentDir != null && Files.exists (aParentDir) && !Files.isWritable
// (aParentDir))
// return EFileIOErrorCode.SOURCE_PARENT_NOT_WRITABLE.getAsIOError
// (EFileIOOperation.CREATE_DIR, aRealDir);
//
// return _perform (EFileIOOperation.CREATE_DIR, Files::createDirectory,
// aRealDir);
}
public CSSPropertyEnumOrNumbers (@Nonnull final ECSSProperty eProp,
@Nullable final ECSSVendorPrefix eVendorPrefix,
@Nullable final ICSSPropertyCustomizer aCustomizer,
final boolean bWithPercentage,
@Nonnegative final int nMinNumbers,
@Nonnegative final int nMaxNumbers,
@Nonnull @Nonempty final Iterable <String> aEnumValues)
{
super (eProp, eVendorPrefix, aCustomizer, aEnumValues);
ValueEnforcer.isGT0 (nMinNumbers, "MinNumbers");
ValueEnforcer.isGT0 (nMaxNumbers, "MaxNumbers");
if (nMaxNumbers < nMinNumbers)
throw new IllegalArgumentException ("MaxNumbers (" +
nMaxNumbers +
") must be >= MinNumbers (" +
nMinNumbers +
")");
m_bWithPercentage = bWithPercentage;
m_nMinNumbers = nMinNumbers;
m_nMaxNumbers = nMaxNumbers;
}
@Nonnull
public CSSStyleRule addSelector (@Nonnegative final int nIndex, @Nonnull final CSSSelector aSelector)
{
ValueEnforcer.isGE0 (nIndex, "Index");
ValueEnforcer.notNull (aSelector, "Selector");
if (nIndex >= getSelectorCount ())
m_aSelectors.add (aSelector);
else
m_aSelectors.add (nIndex, aSelector);
return this;
}
/**
* @param sPrefix
* The extension to use. May neither be <code>null</code> nor empty.
* @return The created {@link IFileFilter}. Never <code>null</code>.
*/
@Nonnull
static IFileFilter filenameStartsWith (@Nonnull @Nonempty final String sPrefix)
{
ValueEnforcer.notEmpty (sPrefix, "Prefix");
return aFile -> {
if (aFile != null)
{
final String sSecureFilename = FilenameHelper.getSecureFilename (aFile.getName ());
if (sSecureFilename != null)
return sSecureFilename.startsWith (sPrefix);
}
return false;
};
}
public SchematronTestFile (@Nonnull @Nonempty final String sParentDirBaseName,
@Nonnull final IReadableResource aRes,
@Nonnull @Nonempty final String sFileBaseName)
{
ValueEnforcer.notEmpty (sParentDirBaseName, "ParentDirBaseName");
ValueEnforcer.notNull (aRes, "Resource");
ValueEnforcer.notEmpty (sFileBaseName, "FileBaseName");
m_sParentDirBaseName = sParentDirBaseName;
m_aRes = aRes;
m_sFileBaseName = sFileBaseName;
}
@Nonnull
public CSSSupportsConditionNested addMember (@Nonnegative final int nIndex,
@Nonnull final ICSSSupportsConditionMember aMember)
{
ValueEnforcer.isGE0 (nIndex, "Index");
ValueEnforcer.notNull (aMember, "SupportsConditionMember");
if (nIndex >= getMemberCount ())
m_aMembers.add (aMember);
else
m_aMembers.add (nIndex, aMember);
return this;
}
@Nonnull
public BigDecimal multiply (@Nonnull final BigDecimal aMultiplicand)
{
ValueEnforcer.notNull (aMultiplicand, "Multiplicand");
m_aValue = m_aValue.multiply (aMultiplicand);
onAfterChange ();
return m_aValue;
}
@Nonnull
public CSSRGBA setGreen (@Nonnull @Nonempty final String sGreen)
{
ValueEnforcer.notEmpty (sGreen, "Green");
m_sGreen = sGreen;
return this;
}
public IntIntMap (final int nSize, final float fFillFactor)
{
ValueEnforcer.isBetweenInclusive (fFillFactor, "FillFactor", 0f, 1f);
ValueEnforcer.isGT0 (nSize, "Size");
final int nCapacity = MapHelper.arraySize (nSize, fFillFactor);
m_nMask = nCapacity - 1;
m_fFillFactor = fFillFactor;
m_aKeys = new int [nCapacity];
m_aValues = _createValueArray (nCapacity);
m_nThreshold = (int) (nCapacity * fFillFactor);
}
@Nonnull
public ESuccess writeSettings (@Nonnull final ISettings aSettings, @Nonnull @WillClose final OutputStream aOS)
{
ValueEnforcer.notNull (aOS, "OutputStream");
try
{
final NonBlockingProperties aProps = new NonBlockingProperties ();
// Must not be sorted, as Properties sorts them as it wishes...
for (final Map.Entry <String, Object> aEntry : aSettings.entrySet ())
{
final String sName = aEntry.getKey ();
final Object aValue = aEntry.getValue ();
if (aValue instanceof ISettings)
throw new IllegalArgumentException ("When saving settings to a Properties object, it may not contained nested settings! Now the key '" +
sName +
"' is mapped to a nested ISettings object!");
final String sValue = TypeConverter.convert (aValue, String.class);
aProps.put (sName, sValue);
}
// Does not close the output stream!
aProps.store (aOS, aSettings.getName ());
return ESuccess.SUCCESS;
}
catch (final IOException ex)
{
LOGGER.error ("Failed to write settings to properties file", ex);
return ESuccess.FAILURE;
}
finally
{
StreamHelper.close (aOS);
}
}
/**
* Get the primary (=first) mime type that is associated to the specified
* filename extension.
*
* @param sExtension
* The filename extension to search. May not be <code>null</code>.
* @return <code>null</code> if no mime type is associated with the passed
* extension.
*/
@Nullable
public String getPrimaryMimeTypeStringForExtension (@Nonnull final String sExtension)
{
ValueEnforcer.notNull (sExtension, "Extension");
final ICommonsList <MimeTypeInfo> aInfos = getAllInfosOfExtension (sExtension);
if (aInfos != null && aInfos.isNotEmpty ())
return aInfos.getFirst ().getPrimaryMimeTypeString ();
return null;
}
@Nonnull
@CheckReturnValue
public SizeLong getScaledToHeight (@Nonnegative final long nNewHeight)
{
ValueEnforcer.isGT0 (nNewHeight, "NewHeight");
if (m_nHeight == nNewHeight)
return this;
final double dMultFactory = MathHelper.getDividedDouble (nNewHeight, m_nHeight);
return new SizeLong ((long) (m_nWidth * dMultFactory), nNewHeight);
}
public static void appendStackToString (@Nonnull final StringBuilder aSB,
@Nonnull final StackTraceElement [] aStackTraceElements,
@Nonnull final String sLineSeparator)
{
ValueEnforcer.notNull (aSB, "StringBuilder");
ValueEnforcer.notNull (aStackTraceElements, "StackTraceElements");
ValueEnforcer.notNull (sLineSeparator, "LineSeparator");
_appendSingleStackTraceToString (aSB, aStackTraceElements, null, true, sLineSeparator);
}
@Nonnull
public EValidity getSchematronValidity (@Nonnull final Node aXMLNode,
@Nullable final String sBaseURI) throws Exception
{
ValueEnforcer.notNull (aXMLNode, "XMLNode");
if (!isValidSchematron ())
return EValidity.INVALID;
return getOrCreateBoundSchema ().validatePartially (aXMLNode, sBaseURI);
}
/**
* Rename a file.
*
* @param aSourceFile
* The original file name. May not be <code>null</code>.
* @param aTargetFile
* The destination file name. May not be <code>null</code>.
* @return A non-<code>null</code> error code.
*/
@Nonnull
public static FileIOError renameFile (@Nonnull final Path aSourceFile, @Nonnull final Path aTargetFile)
{
ValueEnforcer.notNull (aSourceFile, "SourceFile");
ValueEnforcer.notNull (aTargetFile, "TargetFile");
final Path aRealSourceFile = _getUnifiedPath (aSourceFile);
final Path aRealTargetFile = _getUnifiedPath (aTargetFile);
// Does the source file exist?
if (!aRealSourceFile.toFile ().isFile ())
return EFileIOErrorCode.SOURCE_DOES_NOT_EXIST.getAsIOError (EFileIOOperation.RENAME_FILE, aRealSourceFile);
// Are source and target different?
if (EqualsHelper.equals (aRealSourceFile, aRealTargetFile))
return EFileIOErrorCode.SOURCE_EQUALS_TARGET.getAsIOError (EFileIOOperation.RENAME_FILE, aRealSourceFile);
// Does the target file already exist?
if (aRealTargetFile.toFile ().exists ())
return EFileIOErrorCode.TARGET_ALREADY_EXISTS.getAsIOError (EFileIOOperation.RENAME_FILE, aRealTargetFile);
// Is the source parent directory writable?
final Path aSourceParentDir = aRealSourceFile.getParent ();
if (aSourceParentDir != null && !Files.isWritable (aSourceParentDir))
return EFileIOErrorCode.SOURCE_PARENT_NOT_WRITABLE.getAsIOError (EFileIOOperation.RENAME_FILE, aRealSourceFile);
// Is the target parent directory writable?
final Path aTargetParentDir = aRealTargetFile.getParent ();
if (aTargetParentDir != null && aTargetParentDir.toFile ().exists () && !Files.isWritable (aTargetParentDir))
return EFileIOErrorCode.TARGET_PARENT_NOT_WRITABLE.getAsIOError (EFileIOOperation.RENAME_FILE, aRealTargetFile);
// Ensure parent of target directory is present
PathHelper.ensureParentDirectoryIsPresent (aRealTargetFile);
return _perform (EFileIOOperation.RENAME_FILE, PathOperations::_atomicMove, aRealSourceFile, aRealTargetFile);
}
/**
* Add a new <code>@namespace</code> rule at the end of the
* <code>@namespace</code> rule list.
*
* @param aNamespaceRule
* The namespace rule to be added. May not be <code>null</code>.
* @return this
*/
@Nonnull
public CascadingStyleSheet addNamespaceRule (@Nonnull final CSSNamespaceRule aNamespaceRule)
{
ValueEnforcer.notNull (aNamespaceRule, "NamespaceRule");
m_aNamespaceRules.add (aNamespaceRule);
return this;
}
public void addForeignElement (@Nonnull final IMicroElement aForeignElement)
{
ValueEnforcer.notNull (aForeignElement, "ForeignElement");
if (aForeignElement.hasParent ())
throw new IllegalArgumentException ("ForeignElement already has a parent!");
m_aContent.add (aForeignElement);
}
@Nonnull
public JsonParser setTabSize (@Nonnegative final int nTabSize)
{
ValueEnforcer.isGT0 (nTabSize, "TabSize");
m_nTabSize = nTabSize;
return this;
}
@Nonnull
public static IMutableStatisticsHandlerTimer getTimerHandler (@Nonnull final Class <?> aClass)
{
ValueEnforcer.notNull (aClass, "Class");
return getTimerHandler (aClass.getName ());
}
@Nonnull
public final CSSWriterSettings setCSSVersion (@Nonnull final ECSSVersion eCSSVersion)
{
ValueEnforcer.notNull (eCSSVersion, "CSSVersion");
m_eCSSVersion = eCSSVersion;
return this;
}
@Nonnull
public String getParametersAsString (@Nonnull final EMimeQuoting eQuotingAlgorithm)
{
ValueEnforcer.notNull (eQuotingAlgorithm, "QuotingAlgorithm");
if (m_aParameters.isEmpty ())
return "";
return _getParametersAsString (eQuotingAlgorithm);
}