/** * Ensures that {@code index} specifies a valid <i>element</i> in an array, * list or string of size {@code size}. An element index may range from zero, * inclusive, to {@code size}, exclusive. * * @param index a user-supplied index identifying an element of an array, list * or string * @param size the size of that array, list or string * @param desc the text to use to describe this index in an error message * @throws IndexOutOfBoundsException if {@code index} is negative or is not * less than {@code size} * @throws IllegalArgumentException if {@code size} is negative */ public static void checkElementIndex(int index, int size, String desc) { checkArgument(size >= 0, "negative size: %s", size); if (index < 0) { throw new IndexOutOfBoundsException( format("%s (%s) must not be negative", desc, index)); } if (index >= size) { throw new IndexOutOfBoundsException( format("%s (%s) must be less than size (%s)", desc, index, size)); } }
/** * Ensures that {@code index} specifies a valid <i>position</i> in an array, * list or string of size {@code size}. A position index may range from zero * to {@code size}, inclusive. * * @param index a user-supplied index identifying a position in an array, list * or string * @param size the size of that array, list or string * @param desc the text to use to describe this index in an error message * @throws IndexOutOfBoundsException if {@code index} is negative or is * greater than {@code size} * @throws IllegalArgumentException if {@code size} is negative */ public static void checkPositionIndex(int index, int size, String desc) { checkArgument(size >= 0, "negative size: %s", size); if (index < 0) { throw new IndexOutOfBoundsException(format( "%s (%s) must not be negative", desc, index)); } if (index > size) { throw new IndexOutOfBoundsException(format( "%s (%s) must not be greater than size (%s)", desc, index, size)); } }
/** * Checks that the datatype of this element allows setting the value to the * given object. Throws an {@link IllegalArgumentException} if the value * is not valid for this element. */ Object checkValue(ElementKey<?, ?> elementKey, Object newValue) { if (newValue != null) { Class<?> datatype = elementKey.getDatatype(); Preconditions.checkArgument(datatype != Void.class, "Element must not contain a text node"); Preconditions.checkArgument(datatype.isInstance(newValue), "Invalid class: %s", newValue.getClass().getCanonicalName()); } return newValue; }
/** * Constructs an empty element creator. */ ElementCreatorImpl(MetadataRegistry registry, TransformKey transformKey) { super(registry, transformKey); MetadataKey<?> key = transformKey.getKey(); Preconditions.checkArgument(key instanceof ElementKey<?, ?>, "Key must be to an element."); this.elementKey = (ElementKey<?, ?>) key; }
/** * Constructs an empty element creator. */ ElementCreatorImpl(MetadataRegistry registry, TransformKey transformKey) { super(registry, transformKey); MetadataKey<?> key = transformKey.getKey(); Preconditions.checkArgument(key instanceof ElementKey<?, ?>, "Key must be to an element."); this.elementKey = (ElementKey<?, ?>) key; }
/** * Constructs an empty element creator. */ ElementCreatorImpl(MetadataRegistry registry, TransformKey transformKey) { super(registry, transformKey); MetadataKey<?> key = transformKey.getKey(); Preconditions.checkArgument(key instanceof ElementKey<?, ?>, "Key must be to an element."); this.elementKey = (ElementKey<?, ?>) key; }
@Override public long skip(long n) throws IOException { Preconditions.checkArgument(n >= 0, "n is negative"); if (n > 0) { while (current != null) { long result = current.skip(n); if (result > 0) { return result; } advance(); } } return 0; }
@Override public long skip(long n) throws IOException { Preconditions.checkArgument(n >= 0, "n is negative"); if (n > 0) { while (current != null) { long result = current.skip(n); if (result > 0) { return result; } advance(); } } return 0; }
@Override public long skip(long n) throws IOException { Preconditions.checkArgument(n >= 0, "n is negative"); if (n > 0) { while (current != null) { long result = current.skip(n); if (result > 0) { return result; } advance(); } } return 0; }
private void generateAtom(OutputGenerator<?> generator, OutputStream outputStream, OutputProperties outputProperties, Object source) throws IOException { // Make sure the parser will accept any IAtom type Preconditions.checkArgument( generator.getSourceType().isAssignableFrom(IAtom.class), "Generator does not handle atom content"); Preconditions.checkArgument(source instanceof IAtom, "Source object must be Atom content"); IAtom atomSource = (IAtom) source; @SuppressWarnings("unchecked") // safe given above check OutputGenerator<IAtom> atomGenerator = (OutputGenerator<IAtom>) generator; atomGenerator.generate(outputStream, outputProperties, atomSource); }
/** * Set URI to send report to. * * @throws NullPointerException if {@code extendedHelp} is {@code null}. * @throws IllegalArgumentException if {@code extendedHelp} isn't a valid * google URI. */ public ErrorElement setSendReport(String sendReport) { Preconditions.checkNotNull(sendReport, "Send report uri must not be null."); Preconditions.checkArgument( sendReport.matches(GOOGLE_URI_PATTERN), "Invalid send report URI: %s", sendReport); errorSendReport = sendReport; return this; }
public <R extends MediaSource> R parse(ParseSource parseSource, InputProperties inProps, Class<R> resultClass) { // Ensure that the expected result type is MediaSource Preconditions.checkArgument(resultClass.isAssignableFrom(MediaSource.class), "Result class must be " + MediaSource.class.getName()); InputStream inputStream = parseSource.getInputStream(); Preconditions.checkNotNull(inputStream, "Parse source must be stream-based"); MediaStreamSource mediaSource = new MediaStreamSource(inputStream, inProps.getContentType().toString()); return resultClass.cast(mediaSource); } }
public <R extends MediaSource> R parse(ParseSource parseSource, InputProperties inProps, Class<R> resultClass) { // Ensure that the expected result type is MediaSource Preconditions.checkArgument(resultClass.isAssignableFrom(MediaSource.class), "Result class must be " + MediaSource.class.getName()); InputStream inputStream = parseSource.getInputStream(); Preconditions.checkNotNull(inputStream, "Parse source must be stream-based"); MediaStreamSource mediaSource = new MediaStreamSource(inputStream, inProps.getContentType().toString()); return resultClass.cast(mediaSource); } }
/** * Returns the element creator at the end of the path by looking it up * directly in the registry (not traversing). */ private ElementCreatorImpl getElementCreator(Path path) { Preconditions.checkArgument(path.selectsElement(), "Must be an element path"); ElementKey<?, ?> parent = path.getParentKey(); if (parent == null) { parent = elementKey; } ElementKey<?, ?> selected = path.getSelectedElementKey(); return (ElementCreatorImpl) registry.build( parent, selected, transformKey.getContext()); }
/** * Adds a virtual attribute, which marks the source attribute as moved and * creates a new virtual attribute in this element with the source path. */ public AttributeCreator moveAttribute(AttributeKey<?> attKey, Path path) { Preconditions.checkArgument(path.selectsAttribute(), "Path must refer to an attribute."); AttributeCreatorImpl dest = replaceAttribute(attKey); AttributeCreatorImpl source = getAttributeCreator(path); dest.setSource(path, source.getTransformKey()); source.moved(); return dest; }
/** * Adds a virtual attribute, which marks the source attribute as moved and * creates a new virtual attribute in this element with the source path. */ public AttributeCreator moveAttribute(AttributeKey<?> attKey, Path path) { Preconditions.checkArgument(path.selectsAttribute(), "Path must refer to an attribute."); AttributeCreatorImpl dest = replaceAttribute(attKey); AttributeCreatorImpl source = getAttributeCreator(path); dest.setSource(path, source.getTransformKey()); source.moved(); return dest; }
/** * Adds a virtual element, which marks the source element as moved and creates * a new virtual child element with the source path. */ public ElementCreator moveElement(ElementKey<?, ?> childKey, Path path) { Preconditions.checkArgument(path.selectsElement(), "Path must refer to an element."); ElementCreatorImpl dest = replaceElement(childKey); ElementCreatorImpl source = getElementCreator(path); dest.setSource(path, source.getTransformKey()); source.moved(); return dest; }
/** * Adds a virtual attribute, which marks the source attribute as moved and * creates a new virtual attribute in this element with the source path. */ public AttributeCreator moveAttribute(AttributeKey<?> attKey, Path path) { Preconditions.checkArgument(path.selectsAttribute(), "Path must refer to an attribute."); AttributeCreatorImpl dest = replaceAttribute(attKey); AttributeCreatorImpl source = getAttributeCreator(path); dest.setSource(path, source.getTransformKey()); source.moved(); return dest; }
/** * Adds a virtual element, which marks the source element as moved and creates * a new virtual child element with the source path. */ public ElementCreator moveElement(ElementKey<?, ?> childKey, Path path) { Preconditions.checkArgument(path.selectsElement(), "Path must refer to an element."); ElementCreatorImpl dest = replaceElement(childKey); ElementCreatorImpl source = getElementCreator(path); dest.setSource(path, source.getTransformKey()); source.moved(); return dest; }
/** * Adds a virtual element, which marks the source element as moved and creates * a new virtual child element with the source path. */ public ElementCreator moveElement(ElementKey<?, ?> childKey, Path path) { Preconditions.checkArgument(path.selectsElement(), "Path must refer to an element."); ElementCreatorImpl dest = replaceElement(childKey); ElementCreatorImpl source = getElementCreator(path); dest.setSource(path, source.getTransformKey()); source.moved(); return dest; }