/** * Verifies that the given {@code CharSequence} is not {@code null} or empty. * * @param s the given {@code CharSequence}. * @return the validated {@code CharSequence}. * @throws NullPointerException if the given {@code CharSequence} is {@code null}. * @throws IllegalArgumentException if the given {@code CharSequence} is empty. */ public static CharSequence checkNotNullOrEmpty(CharSequence s) { return checkNotNullOrEmpty(s, ARGUMENT_EMPTY); }
/** * Verifies that the given {@code CharSequence} is not {@code null} or empty. * * @param s the given {@code CharSequence}. * @return the validated {@code CharSequence}. * @throws NullPointerException if the given {@code CharSequence} is {@code null}. * @throws IllegalArgumentException if the given {@code CharSequence} is empty. */ public static CharSequence checkNotNullOrEmpty(CharSequence s) { return checkNotNullOrEmpty(s, ARGUMENT_EMPTY); }
/** * Returns result of given method invocation on provided object. * <p> * Following requirements have to be met to extract method results: * <ul> * <li>method has to be public,</li> * <li>method cannot accept any arguments,</li> * <li>method cannot return void.</li> * </ul> * * @param instance object on which * @param methodName name of method to be invoked * @return result of method invocation * @throws IllegalArgumentException if method does not exist or is not public, method returns void or method accepts * any argument */ public static Object methodResultFor(Object instance, String methodName) { checkNotNull(instance, "Object instance can not be null!"); checkNotNullOrEmpty(methodName, "Method name can not be empty!"); Method method = findMethod(methodName, instance.getClass()); return invokeMethod(instance, method); }
/** * Returns the getter {@link Method} for a property matching the given name in the given object. * * @param propertyName the given property name. * @param target the given object. * @return the getter {@code Method} for a property matching the given name in the given object. * @throws NullPointerException if the given property name is {@code null}. * @throws IllegalArgumentException if the given property name is empty. * @throws NullPointerException if the given object is {@code null}. * @throws IntrospectionError if the getter for the matching property cannot be found or accessed. */ public static Method getPropertyGetter(String propertyName, Object target) { checkNotNullOrEmpty(propertyName); checkNotNull(target); Method getter; try { getter = findGetter(propertyName, target); if (Modifier.isPublic(getter.getModifiers())) { // force access for static class with public getter getter.setAccessible(true); } getter.invoke(target); } catch (Exception t) { throw new IntrospectionError(propertyNotFoundErrorMessage(propertyName, target), t); } return getter; }
/** * Returns the getter {@link Method} for a property matching the given name in the given object. * * @param propertyName the given property name. * @param target the given object. * @return the getter {@code Method} for a property matching the given name in the given object. * @throws NullPointerException if the given property name is {@code null}. * @throws IllegalArgumentException if the given property name is empty. * @throws NullPointerException if the given object is {@code null}. * @throws IntrospectionError if the getter for the matching property cannot be found or accessed. */ public static Method getPropertyGetter(String propertyName, Object target) { checkNotNullOrEmpty(propertyName); checkNotNull(target); Method getter; try { getter = findGetter(propertyName, target); if (Modifier.isPublic(getter.getModifiers())) { // force access for static class with public getter getter.setAccessible(true); } getter.invoke(target); } catch (Exception t) { throw new IntrospectionError(propertyNotFoundErrorMessage(propertyName, target), t); } return getter; }
/** * Returns result of given method invocation on provided object. * <p> * Following requirements have to be met to extract method results: * <ul> * <li>method has to be public,</li> * <li>method cannot accept any arguments,</li> * <li>method cannot return void.</li> * </ul> * * @param instance object on which * @param methodName name of method to be invoked * @return result of method invocation * @throws IllegalArgumentException if method does not exist or is not public, method returns void or method accepts * any argument */ public static Object methodResultFor(Object instance, String methodName) { checkNotNull(instance, "Object instance can not be null!"); checkNotNullOrEmpty(methodName, "Method name can not be empty!"); Method method = findMethod(methodName, instance.getClass()); return invokeMethod(instance, method); }
/** * Verifies that the given {@code String} is not {@code null} or empty. * * @param s the given {@code String}. * @return the validated {@code String}. * @throws NullPointerException if the given {@code String} is {@code null}. * @throws IllegalArgumentException if the given {@code String} is empty. */ public static String checkNotNullOrEmpty(String s) { return checkNotNullOrEmpty(s, ARGUMENT_EMPTY); }
/** * Creates a new {@link PatternTextMatcher}. * * @param patterns the regular expression patterns to match. * @throws NullPointerException if the array of patterns is {@code null}. * @throws IllegalArgumentException if the array of patterns is empty. */ public PatternTextMatcher(@Nonnull Pattern... patterns) { this.patterns = checkNotNullOrEmpty(patterns); }
/** * Creates a new {@link StringTextMatcher}. * * @param values the {@code String} values to match. Each value can be a regular expression. * @throws NullPointerException if the array of values is {@code null}. * @throws IllegalArgumentException if the array of values is empty. */ public StringTextMatcher(@Nonnull String... values) { this.values = checkNotNullOrEmpty(values); }
private BeanPropertiesComparator(final String... propNames) { Preconditions.checkNotNullOrEmpty(propNames); final ArrayList<String> args = new ArrayList<String>(); Collections.addAll(args, propNames); this.propNames = Collections.unmodifiableList(args); this.methods = null; }
/** * Creates a new {@link NameMatcher}. * * @param name the name of the AWT or Swing {@code Component} we are looking for. * @param type the type of the {@code Component} we are looking for. * @param requireShowing indicates if the {@code Component} to match should be showing or not. * @throws NullPointerException if the given name is empty. * @throws IllegalArgumentException if the given name is empty. * @throws NullPointerException if the given type is {@code null}. */ public NameMatcher(@Nullable String name, @Nonnull Class<? extends Component> type, boolean requireShowing) { super(requireShowing); this.name = checkNotNullOrEmpty(name).toString(); this.type = checkNotNull(type); }
/** * Creates a new {@link LabelMatcher}. * * @param label the text of the label associated to the AWT or Swing {@code Component} we are looking for. * @param type the type of the {@code Component} we are looking for. * @param requireShowing indicates if the {@code Component} to match should be showing or not. * @throws NullPointerException if the given label is {@code null}. * @throws IllegalArgumentException if the given label is empty. * @throws NullPointerException if the given type is {@code null}. */ public LabelMatcher(@Nullable String label, @Nonnull Class<? extends Component> type, boolean requireShowing) { super(requireShowing); this.label = checkNotNullOrEmpty(label).toString(); this.type = checkNotNull(type); }
static @Nonnull String messageTypeAsText(int messageType) { if (messageMap.containsKey(messageType)) { return checkNotNullOrEmpty(messageMap.get(messageType)).toString(); } throw actionFailure(concat("The message type <", messageType, "> is not valid")); }
@Override public void saveImage(@Nonnull BufferedImage image, @Nonnull String filePath) { Preconditions.checkNotNullOrEmpty(filePath); if (!filePath.endsWith(PNG)) { String format = String.format("The file in path '%s' should have extension 'png'", filePath); throw new IllegalArgumentException(format); } try { writer.writeAsPng(image, filePath); } catch (IOException e) { String msg = String.format("Unable to save image as '%s'", filePath); throw new IORuntimeException(msg, e); } } }
/** * Returns a {@code Color} from the given {@code String} containing the hexadecimal coding of a color. * * @param hexString contains the hexadecimal coding of a color. * @return a {@code Color} from the given {@code String} containing the hexadecimal coding of a color. * @throws NullPointerException if the hexadecimal code is {@code null}. * @throws IllegalArgumentException if the hexadecimal code is empty. * @throws NumberFormatException if the hexadecimal code is empty. */ @Nonnull public static Color colorFromHexString(@Nonnull String hexString) { checkNotNullOrEmpty(hexString); try { return new Color(Integer.parseInt(hexString, 16)); } catch (NumberFormatException e) { throw new NumberFormatException(concat("The hexadecimal code ", quote(hexString), " is not a valid color code")); } }
/** * Creates a new {@link AppletLauncher}. The {@code Applet} to launch is a new instance of the given type. It is * assumed that the given type has a default constructor. * * @param appletType the type of {@code Applet} to instantiate. * @return the created {@code AppletLauncher}. * @throws NullPointerException if the given type name is {@code null}. * @throws IllegalArgumentException if the given type name is empty. * @throws IllegalArgumentException if the given type is not a subclass of {@code Applet}. * @throws UnexpectedException if the given type cannot be loaded. * @throws UnexpectedException if a new instance of the given type cannot be instantiated. */ @RunsInEDT @Nonnull public static AppletLauncher applet(@Nonnull String appletType) { checkNotNullOrEmpty(appletType); Class<?> type = load(appletType); if (!(Applet.class.isAssignableFrom(type))) { String msg = String.format("The given type is not a subclass of %s", Applet.class.getName()); throw new IllegalArgumentException(msg); } return instantiate(type); }
/** * Selects the given file in the {@code JFileChooser}. * * @param fileChooser the target {@code JFileChooser}. * @param files the files to select. * @throws NullPointerException if the given array of files is {@code null}. * @throws IllegalArgumentException if the given array of files is empty. * @throws IllegalStateException if this fixture's {@code JFileChooser} is disabled. * @throws IllegalStateException if this fixture's {@code JFileChooser} is not showing on the screen. * @throws IllegalStateException if this fixture's {@code JFileChooser} does not support multiple selection and there * is more than one file to select. * @throws IllegalArgumentException if this fixture's {@code JFileChooser} can select directories only and any of the * files to select is not a directory. * @throws IllegalArgumentException if this fixture's {@code JFileChooser} cannot select directories and any of the * files to select is a directory. */ public void selectFiles(@Nonnull JFileChooser fileChooser, @Nonnull File[] files) { checkNotNull(files); for (File file : checkNotNullOrEmpty(files)) { checkNotNull(file); } setSelectedFiles(fileChooser, files); }
/** * Returns result of given method invocation on provided object. * <p/> * Following requirements have to be met to extract method results: * <ul> * <li>method has to be public,</li> * <li>method cannot accept any arguments,</li> * <li>method cannot return void.</li> * </ul> * * @param instance object on which * @param methodName name of method to be invoked * @return result of method invocation * @throws IllegalArgumentException if method does not exist or is not public, method returns void or method accepts * any argument */ public static Object methodResultFor(Object instance, String methodName) { checkNotNull(instance, "Object instance can not be null!"); checkNotNullOrEmpty(methodName, "Method name can not be empty!"); Method method = findMethod(methodName, instance.getClass()); return invokeMethod(instance, method); }
/** * <p> * Creates a {@link KeyStrokeMappingProvider} containing all the character-keystroke mappings specified in the file * with the given name. * </p> * * <p> * <strong>Note:</strong> This attempts to read the file using {@link ClassLoader#getResourceAsStream(String)}. * </p> * * @param fileName the name of the file to parse. * @return the created {@code KeyStrokeMappingProvider}. * @throws NullPointerException if the given name is {@code null}. * @throws IllegalArgumentException if the given name is empty. * @throws ParsingException if any error occurs during parsing. * @see #parse(File) */ @Nonnull public KeyStrokeMappingProvider parse(@Nonnull String fileName) { checkNotNullOrEmpty(fileName); try { return parse(fileAsStream(fileName)); } catch (IOException e) { throw new ParsingException(concat("An I/O error ocurred while parsing file ", fileName), e); } }
/** * Returns a {@link PropertyDescriptor} for a property matching the given name in the given object. * * @param propertyName the given property name. * @param target the given object. * @return a {@code PropertyDescriptor} for a property matching the given name in the given object. * @throws NullPointerException if the given property name is {@code null}. * @throws IllegalArgumentException if the given property name is empty. * @throws NullPointerException if the given object is {@code null}. * @throws IntrospectionError if a matching property cannot be found or accessed. */ public static PropertyDescriptor getProperty(String propertyName, Object target) { checkNotNullOrEmpty(propertyName); checkNotNull(target); PropertyDescriptor prop = getBeanProperty(target.getClass(), propertyName); // if not a java 7 property, check if there is a default getter method if (prop == null) prop = digForDefaultImplementations(target.getClass(), propertyName); if (prop != null) return prop; else throw new IntrospectionError(propertyNotFoundErrorMessage(propertyName, target)); }