Example usage for java.util Objects requireNonNull

List of usage examples for java.util Objects requireNonNull

Introduction

In this page you can find the example usage for java.util Objects requireNonNull.

Prototype

public static <T> T requireNonNull(T obj) 

Source Link

Document

Checks that the specified object reference is not null .

Usage

From source file:io.fabric8.vertx.maven.plugin.FileFilterMain.java

public static void main(String[] args) {

    Commandline commandline = new Commandline();
    commandline.setExecutable("java");
    commandline.createArg().setValue("io.vertx.core.Launcher");
    commandline.createArg().setValue("--redeploy=target/**/*");

    System.out.println(commandline);/*from   www  .  ja va 2 s  .co  m*/

    File baseDir = new File("/Users/kameshs/git/fabric8io/vertx-maven-plugin/samples/vertx-demo");
    List<String> includes = new ArrayList<>();
    includes.add("src/**/*.java");
    //FileAlterationMonitor monitor  = null;
    try {

        Set<Path> inclDirs = new HashSet<>();

        includes.forEach(s -> {
            try {

                if (s.startsWith("**")) {
                    Path rootPath = Paths.get(baseDir.toString());
                    if (Files.exists(rootPath)) {
                        File[] dirs = rootPath.toFile().listFiles((dir, name) -> dir.isDirectory());
                        Objects.requireNonNull(dirs);
                        Stream.of(dirs).forEach(f -> inclDirs.add(Paths.get(f.toString())));
                    }
                } else if (s.contains("**")) {
                    String root = s.substring(0, s.indexOf("/**"));
                    Path rootPath = Paths.get(baseDir.toString(), root);
                    if (Files.exists(rootPath)) {
                        File[] dirs = rootPath.toFile().listFiles((dir, name) -> dir.isDirectory());
                        Objects.requireNonNull(dirs);
                        Stream.of(dirs).forEach(f -> inclDirs.add(Paths.get(f.toString())));
                    }
                }

                List<Path> dirs = FileUtils.getFileAndDirectoryNames(baseDir, s, null, true, true, true, true)
                        .stream().map(FileUtils::dirname).map(Paths::get)
                        .filter(p -> Files.exists(p) && Files.isDirectory(p)).collect(Collectors.toList());

                inclDirs.addAll(dirs);

            } catch (Exception e) {
                e.printStackTrace();
            }
        });

        FileAlterationMonitor monitor = fileWatcher(inclDirs);

        Runnable monitorTask = () -> {
            try {
                monitor.start();
            } catch (Exception e) {
                e.printStackTrace();
            }
        };

        monitorTask.run();

    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:io.reactiverse.vertx.maven.plugin.FileFilterMain.java

public static void main(String[] args) {

    Commandline commandline = new Commandline();
    commandline.setExecutable("java");
    commandline.createArg().setValue("io.vertx.core.Launcher");
    commandline.createArg().setValue("--redeploy=target/**/*");

    System.out.println(commandline);/*w ww  .  j  av  a  2 s.c  o m*/

    File baseDir = new File("/Users/kameshs/git/reactiverse/vertx-maven-plugin/samples/vertx-demo");
    List<String> includes = new ArrayList<>();
    includes.add("src/**/*.java");
    //FileAlterationMonitor monitor  = null;
    try {

        Set<Path> inclDirs = new HashSet<>();

        includes.forEach(s -> {
            try {

                if (s.startsWith("**")) {
                    Path rootPath = Paths.get(baseDir.toString());
                    if (Files.exists(rootPath)) {
                        File[] dirs = rootPath.toFile().listFiles((dir, name) -> dir.isDirectory());
                        Objects.requireNonNull(dirs);
                        Stream.of(dirs).forEach(f -> inclDirs.add(Paths.get(f.toString())));
                    }
                } else if (s.contains("**")) {
                    String root = s.substring(0, s.indexOf("/**"));
                    Path rootPath = Paths.get(baseDir.toString(), root);
                    if (Files.exists(rootPath)) {
                        File[] dirs = rootPath.toFile().listFiles((dir, name) -> dir.isDirectory());
                        Objects.requireNonNull(dirs);
                        Stream.of(dirs).forEach(f -> inclDirs.add(Paths.get(f.toString())));
                    }
                }

                List<Path> dirs = FileUtils.getFileAndDirectoryNames(baseDir, s, null, true, true, true, true)
                        .stream().map(FileUtils::dirname).map(Paths::get)
                        .filter(p -> Files.exists(p) && Files.isDirectory(p)).collect(Collectors.toList());

                inclDirs.addAll(dirs);

            } catch (Exception e) {
                e.printStackTrace();
            }
        });

        FileAlterationMonitor monitor = fileWatcher(inclDirs);

        Runnable monitorTask = () -> {
            try {
                monitor.start();
            } catch (Exception e) {
                e.printStackTrace();
            }
        };

        monitorTask.run();

    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:Main.java

private static List<Byte> asList(byte[] bytes) {
    Objects.requireNonNull(bytes);

    if (bytes.length == 0) {
        return new ArrayList<>();
    }//from   ww  w.  j  av a  2 s.  c  om

    final List<Byte> list = new ArrayList<>(bytes.length);
    for (byte b : bytes) {
        list.add(b);
    }
    return list;
}

From source file:Main.java

public static byte[] reverse(final byte[] bytes) {
    Objects.requireNonNull(bytes);

    if (bytes.length == 0) {
        return new byte[0];
    }//w  ww  . ja v  a  2s .  c  o  m

    final List<Byte> list = asList(bytes);
    Collections.reverse(list);
    return toArray(list);
}

From source file:Main.java

/**
 * Convert a {@link Properties} into a {@link HashMap}.<br />
 * @param properties/*from  w  w  w .  ja  v  a 2 s .c o m*/
 *        the properties to convert.
 * @return the map with the same objects.
 */
public static Map<String, String> convertPropertiesToMap(final Properties properties) {
    Objects.requireNonNull(properties);
    final Map<String, String> map = new HashMap<>(properties.size());
    for (final Entry<Object, Object> property : properties.entrySet()) {
        // Properties are always Strings (left as Object in JDK for backward compatibility purposes)
        map.put((String) property.getKey(), (String) property.getValue());
    }
    return map;
}

From source file:Main.java

private static byte[] toArray(List<Byte> bytes) {
    Objects.requireNonNull(bytes);

    if (bytes.size() == 0) {
        return new byte[0];
    }//w  w  w . ja v  a  2  s  . com

    final byte[] array = new byte[bytes.size()];
    for (int i = 0; i < bytes.size(); i++) {
        array[i] = bytes.get(i);
    }
    return array;
}

From source file:Main.java

public static XPathExpression buildXPath(String path, Map<String, String> map) {
    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();
    if (map != null)
        xpath.setNamespaceContext(new NamespaceContext() {

            public Iterator getPrefixes(String namespaceURI) {
                throw new UnsupportedOperationException();
            }// w  w  w .  ja v  a2s.c o m

            public String getPrefix(String namespaceURI) {
                throw new UnsupportedOperationException();
            }

            public String getNamespaceURI(String prefix) {
                Objects.requireNonNull(prefix);
                if (map.containsKey(prefix))
                    return map.get(prefix);
                return XMLConstants.NULL_NS_URI;
            }
        });

    try {
        return xpath.compile(path);
    } catch (XPathExpressionException e) {
        throw new RuntimeException(e);
    }
}

From source file:azkaban.jobtype.javautils.ValidationUtils.java

/**
 * Validates if all of the keys exist of none of them exist
 * @param props//from  ww w  . ja v a  2 s . c  o  m
 * @param keys
 * @throws IllegalArgumentException only if some of the keys exist
 */
public static void validateAllOrNone(Props props, String... keys) {
    Objects.requireNonNull(keys);

    boolean allExist = true;
    boolean someExist = false;
    for (String key : keys) {
        Object val = props.get(key);
        allExist &= val != null;
        someExist |= val != null;
    }

    if (someExist && !allExist) {
        throw new IllegalArgumentException(
                "Either all of properties exist or none of them should exist for " + Arrays.toString(keys));
    }
}

From source file:io.knotx.knot.service.service.ServiceAttributeUtil.java

private static String extract(String attributeName, int groupIndex) {
    Objects.requireNonNull(attributeName);

    Matcher matcher = ATTR_PATTERN.matcher(attributeName);
    if (matcher.matches()) {
        String namespace = matcher.group(groupIndex);
        return StringUtils.defaultString(namespace);
    } else {/*from   w  w  w  . jav  a2s .  co  m*/
        throw new InvalidAttributeException(attributeName);
    }

}

From source file:Main.java

/**
 * This is a shortcut for {@code (Stream<O>) s.filter(o -> clazz.isInstance(o))}.
 *
 * @param s     stream that's elements are filtered by type {@code clazz}. Must not be {@code null}.
 * @param clazz Type the elements of the input stream should be of to be part of the returned result stream.
 *              Must not be {@code null}.
 * @param <I>   Type of elements in input stream
 * @param <O>   Type of element remaining in the output stream
 * @return input stream filtered by type {@code O}
 * @throws NullPointerException when {@code clazz} or {@code s} is {@code null}.
 */// ww w.  j  a va  2s . c  o  m
@SuppressWarnings("unchecked") // we know the cast is safe, since all elements are of type O
public static <I, O> Stream<O> filter(Stream<I> s, Class<O> clazz) throws NullPointerException {
    Objects.requireNonNull(clazz);
    Objects.requireNonNull(s);
    return (Stream<O>) s.filter(clazz::isInstance);
}