Example usage for org.apache.commons.lang3 Validate isInstanceOf

List of usage examples for org.apache.commons.lang3 Validate isInstanceOf

Introduction

In this page you can find the example usage for org.apache.commons.lang3 Validate isInstanceOf.

Prototype

public static void isInstanceOf(final Class<?> type, final Object obj) 

Source Link

Document

Validates that the argument is an instance of the specified class, if not throws an exception.

Usage

From source file:com.alta189.beaker.HandlerPriorityComparator.java

@Override
public int compare(Object o1, Object o2) {
    Validate.notNull(o1);//from w ww.j  av a  2s . c  o  m
    Validate.notNull(o2);

    Validate.isInstanceOf(HandlerRegistration.class, o1);
    Validate.isInstanceOf(HandlerRegistration.class, o2);

    Priority priority1 = CastUtil.safeCast(o1, HandlerRegistration.class).getPriority();
    Priority priority2 = CastUtil.safeCast(o2, HandlerRegistration.class).getPriority();

    if (priority1.getIndex() == priority2.getIndex()) {
        return 0;
    } else if (priority1.getIndex() < priority2.getIndex()) {
        return -1;
    } else if (priority1.getIndex() > priority2.getIndex()) {
        return 1;
    }

    throw new RuntimeException("should not have made it this far");
}

From source file:de.lebenshilfe_muenster.uk_gebaerden_muensterland.sign_browser.AbstractSignBrowserTest.java

public static Matcher<RecyclerView.ViewHolder> getHolderForSignWithName(final String signNameLocaleDe) {
    Validate.notEmpty(signNameLocaleDe);
    return new BaseMatcher<RecyclerView.ViewHolder>() {

        @Override//from  w w  w. j a va 2 s. c  o  m
        public boolean matches(Object item) {
            Validate.isInstanceOf(SignBrowserAdapter.ViewHolder.class, item);
            final SignBrowserAdapter.ViewHolder holder = (SignBrowserAdapter.ViewHolder) item;
            boolean matches = false;
            if (!(null == holder.txtSignName)) {
                matches = ((signNameLocaleDe.equals(holder.txtSignName.getText().toString()))
                        && (View.VISIBLE == holder.txtSignName.getVisibility()));
            }
            return matches;
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("with sign name: " + signNameLocaleDe);
        }
    };
}

From source file:annis.security.ANNISUserRealm.java

@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    Validate.isInstanceOf(String.class, principals.getPrimaryPrincipal());
    String userName = (String) principals.getPrimaryPrincipal();

    Set<String> roles = new TreeSet<String>();
    roles.add(userName);/*from   ww  w .  j a  va  2 s  . c o m*/
    if (!userName.equals(anonymousUser)) {
        Properties userProps = getPropertiesForUser(userName);

        String groupsRaw = userProps.getProperty("groups", "").trim();
        roles.addAll(Arrays.asList(groupsRaw.split("\\s*,\\s*")));

        roles.add(defaultUserRole);
    }
    return new SimpleAuthorizationInfo(roles);
}

From source file:annis.security.ANNISUserRealm.java

@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
    Validate.isInstanceOf(String.class, token.getPrincipal());

    String userName = (String) token.getPrincipal();
    if (userName.equals(anonymousUser)) {
        // for anonymous users the user name equals the Password, so hash the user name
        Sha256Hash hash = new Sha256Hash(userName);
        return new SimpleAuthenticationInfo(userName, hash.getBytes(), ANNISUserRealm.class.getName());
    }/*  ww w . j  a  va2  s.  c  o  m*/

    Properties userProps = getPropertiesForUser(userName);
    if (userProps != null) {
        String password = userProps.getProperty("password");
        if (password != null) {
            if (password.startsWith("$")) {
                Shiro1CryptFormat fmt = new Shiro1CryptFormat();
                Hash hashCredentials = fmt.parse(password);
                if (hashCredentials instanceof SimpleHash) {
                    SimpleHash simpleHash = (SimpleHash) hashCredentials;

                    Validate.isTrue(simpleHash.getIterations() == 1,
                            "Hash iteration count must be 1 for every password hash!");

                    // actually set the information from the user file
                    SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(userName,
                            simpleHash.getBytes(), ANNISUserRealm.class.getName());
                    info.setCredentialsSalt(simpleHash.getSalt());
                    return info;
                }
            } else {
                // fallback unsalted hex hash
                SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(token.getPrincipal(), password,
                        ANNISUserRealm.class.getName());
                return info;
            }

        }
    }
    return null;
}

From source file:atg.tools.dynunit.test.util.FileUtil.java

/**
 * @see org.apache.commons.lang3.SerializationUtils#serialize(java.io.Serializable, java.io.OutputStream)
 *//*w  ww.j  av  a2 s .  c o m*/
@Deprecated
public static void serialize(@NotNull final File file, final Object o) throws IOException {
    Validate.isInstanceOf(Serializable.class, o);
    final Serializable object = (Serializable) o;
    SerializationUtils.serialize(object, new FileOutputStream(file));
}

From source file:net.ostis.scpdev.m4scp.editors.M4ScpMultiPageEditor.java

/**
 * Calculates the contents of page 1 when the it is activated.
 *///from   www .j  ava  2 s.c o m
protected void pageChange(int newPageIndex) {
    super.pageChange(newPageIndex);

    if (newPageIndex == 1) {
        Validate.isInstanceOf(IFileEditorInput.class, m4scpEditor.getEditorInput());
        final IFileEditorInput m4scpInput = (IFileEditorInput) m4scpEditor.getEditorInput();
        final IFile m4scpSource = m4scpInput.getFile();

        final ByteArrayOutputStream out = new ByteArrayOutputStream();

        final Job convertJob = new Job("Convert m4scp to scs") {
            @Override
            protected IStatus run(IProgressMonitor monitor) {
                String m4 = ScCoreModule.getM4Path();
                String m4scp = ScCoreModule.getM4ScpPath();

                ProcessBuilder psb = new ProcessBuilder(m4, m4scp, m4scpSource.getRawLocation().toOSString());

                try {
                    Process ps = psb.start();
                    IOUtils.copy(ps.getInputStream(), out);

                    if (ps.waitFor() == 0)
                        return Status.OK_STATUS;
                } catch (Exception e) {
                    e.printStackTrace();
                }

                return Status.CANCEL_STATUS;
            }
        };

        convertJob.setPriority(Job.INTERACTIVE);
        convertJob.schedule();

        try {
            convertJob.join();
            if (convertJob.getResult() == Status.OK_STATUS)
                scsEditor.setInput(new StorageEditorInput(new InMemoryStorage(out.toString())));
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

From source file:com.premiumminds.billy.core.services.builders.impl.GenericInvoiceBuilderImpl.java

@Override
@NotOnUpdate//from  w  w  w  .  j  a v  a2  s. com
public <T extends GenericInvoiceEntry> TBuilder addEntry(Builder<T> entryBuilder) {
    BillyValidator.notNull(entryBuilder, GenericInvoiceBuilderImpl.LOCALIZER.getString("field.entry"));
    T entry = entryBuilder.build();
    Validate.isInstanceOf(GenericInvoiceEntryEntity.class, entry); // TODO
    // message
    GenericInvoiceEntity i = this.getTypeInstance();
    GenericInvoiceEntryEntity e = (GenericInvoiceEntryEntity) entry;
    i.getEntries().add(e);
    e.setEntryNumber(i.getEntries().size());
    this.validateValues();
    return this.getBuilder();
}

From source file:org.datalorax.populace.core.util.TypeResolver.java

/**
 * Get a stream of available alias for the provided type variable e.g. given a type:
 * <pre>/*from  w  w w.ja  v  a2 s  .  c  om*/
 * {@code
 *    interface SomeInterface&lt;T2&gt; {}
 *    class SomeType&lt;T2&gt; implements SomeInterface&lt;T2&gt; {}
 *
 *    TypeVariable&lt;?&gt; typeVariable = SomeType.class.getTypeParameters()[0];
 *    TypeToken interfaceToken = findTypeToken(TypeToken.of(SomeType.class).getTypes(), SomeInterface.class);
 *    getTypeArgumentAliases(typeVariable, interfaceToken);
 * }
 * </pre>
 * <p>
 * will return a stream containing T2.
 *
 * @param typeVar the type variable to find an alias for
 * @param type    the type token to search for matching type variables
 * @return the type argument of any matching type variables found.
 */
private static Stream<TypeVariable<?>> getTypeArgumentAliases(final TypeVariable<?> typeVar,
        final TypeToken<?> type) {
    Validate.isInstanceOf(ParameterizedType.class, type.getType());
    final Type[] sourceTypeArgs = ((ParameterizedType) type.getType()).getActualTypeArguments();
    final TypeVariable<? extends Class<?>>[] sourceAliases = type.getRawType().getTypeParameters();

    return IntStream.range(0, sourceTypeArgs.length).filter(i -> sourceTypeArgs[i].equals(typeVar)) // Filter out indexes with different type argument
            .filter(i -> !sourceAliases[i].equals(typeVar)) // Filter out aliases that match existing
            .mapToObj(i -> sourceAliases[i]); // Return aliases
}

From source file:org.datalorax.populace.core.walk.inspector.CollectionInspector.java

@SuppressWarnings("unchecked")
private static Collection<?> ensureCollection(final Object instance) {
    Validate.isInstanceOf(Collection.class, instance);
    Validate.isTrue(!Set.class.isAssignableFrom(instance.getClass()), "Set types are not supported");
    return (Collection<?>) instance;
}

From source file:org.datalorax.populace.core.walk.inspector.ListInspector.java

@SuppressWarnings("unchecked")
private static List<Object> ensureList(final Object instance) {
    Validate.isInstanceOf(List.class, instance);
    return (List<Object>) instance;
}