List of usage examples for com.google.common.base Preconditions checkNotNull
public static <T> T checkNotNull(T reference, @Nullable Object errorMessage)
From source file:org.apache.flume.serialization.EventSerializerFactory.java
public static EventSerializer getInstance(String serializerType, Context context, OutputStream out) { Preconditions.checkNotNull(serializerType, "serializer type must not be null"); // try to find builder class in enum of known output serializers EventSerializerType type;/*from www . ja va 2 s . co m*/ try { type = EventSerializerType.valueOf(serializerType.toUpperCase()); } catch (IllegalArgumentException e) { logger.debug("Not in enum, loading builder class: {}", serializerType); type = EventSerializerType.OTHER; } Class<? extends EventSerializer.Builder> builderClass = type.getBuilderClass(); // handle the case where they have specified their own builder in the config if (builderClass == null) { try { Class c = Class.forName(serializerType); if (c != null && EventSerializer.Builder.class.isAssignableFrom(c)) { builderClass = (Class<? extends EventSerializer.Builder>) c; } else { logger.error("Unable to instantiate Builder from {}", serializerType); return null; } } catch (ClassNotFoundException ex) { logger.error("Class not found: " + serializerType, ex); return null; } } // build the builder EventSerializer.Builder builder; try { builder = builderClass.newInstance(); } catch (InstantiationException ex) { logger.error("Cannot instantiate builder: " + serializerType, ex); return null; } catch (IllegalAccessException ex) { logger.error("Cannot instantiate builder: " + serializerType, ex); return null; } return builder.build(context, out); }
From source file:com.google.security.zynamics.binnavi.Gui.GraphWindows.Implementations.CEdgeDeleter.java
/** * Removes an edge from a graph./*from ww w . j a va 2 s . c o m*/ * * @param view The view from which the edge is removed. * @param edge The edge to remove from the graph. */ public static void deleteEdge(final INaviView view, final INaviEdge edge) { Preconditions.checkNotNull(view, "IE01727: View argument can not be null"); Preconditions.checkNotNull(edge, "IE01728: Edge argument can not be null"); view.getContent().deleteEdge(edge); }
From source file:net.scriptability.core.util.StringUtils.java
/** * Returns a line by number from a source string. Handles CR+LF, CR and LF line separators. * * @param source source string//w ww. j a v a2s . c o m * @param lineNumber line number to return from source string * @return string representing the line at the given line number */ public static String getLine(final String source, final int lineNumber) { Preconditions.checkNotNull(source, "Source cannot be null."); Preconditions.checkArgument(lineNumber > 0, "Line must be greater than 0."); String[] lines; if (source.contains(CR_LF)) { lines = source.split(CR_LF); } else if (source.contains(CR)) { lines = source.split(CR); } else if (source.contains(LF)) { lines = source.split(LF); } else { lines = new String[] { source }; } if (lineNumber > lines.length) { throw new IllegalArgumentException( "Requested line number is greater than the number of lines in source string."); } return lines[lineNumber - 1]; }
From source file:org.haiku.haikudepotserver.dataobjects.Country.java
public static Optional<Country> tryGetByCode(ObjectContext context, final String code) { Preconditions.checkNotNull(context, "the context must be provided"); Preconditions.checkState(!Strings.isNullOrEmpty(code)); return getAll(context).stream().filter(a -> a.getCode().equals(code)).collect(SingleCollector.optional()); }
From source file:de.cosmocode.issuetracker.activecollab.ActiveCollabConnector.java
/** * Connects to an ActiveCollab server./*from w w w . j a v a2 s.c o m*/ * * @since 1.0 * @param uri the uri to connect to * @param token the api token * @param projectId the project identifier * @return an {@link ActiveCollab} instance */ public static ActiveCollab connectActiveCollab(URI uri, String token, int projectId) { Preconditions.checkNotNull(uri, "URI"); Preconditions.checkNotNull(token, "Token"); return new DefaultActiveCollab(uri, token, projectId); }
From source file:com.aionemu.gameserver.utils.audit.AuditLogger.java
public static final void info(Player player, String message) { Preconditions.checkNotNull(player, "Player should not be null or use different info method"); if (LoggingConfig.LOG_AUDIT) { info(player.getName(), player.getObjectId(), message); }/* w w w . j a va2 s .c o m*/ if (PunishmentConfig.PUNISHMENT_ENABLE) { AutoBan.punishment(player, message); } }
From source file:com.google.security.zynamics.binnavi.ZyGraph.Builders.ZyTextNodeBuilder.java
/** * Builds the content of the text node./* w ww. j a v a 2 s . com*/ * * @param node The node for which the context is built. * * @return The created node content. */ public static ZyLabelContent buildContent(final INaviTextNode node) { Preconditions.checkNotNull(node, "IE01700: Node argument can not be null"); final ZyLabelContent content = new ZyLabelContent(null); buildContent(content, node); return content; }
From source file:com.github.jeluard.guayaba.lang.Runnables.java
/** * @param runnable//from w w w . ja v a2 s. c o m * @param logger * @return specified {@link Runnable} in a new {@link Runnable} that will intercept and log any exception thrown by {@link Runnable#run()} */ public static Runnable loggingExecutionFailure(final Runnable runnable, final Logger logger) { Preconditions.checkNotNull(runnable, "null runnable"); Preconditions.checkNotNull(logger, "null logger"); return new Runnable() { @Override public void run() { try { runnable.run(); } catch (Exception e) { if (logger.isLoggable(Level.WARNING)) { logger.log(Level.WARNING, "Exception while executing <" + runnable + ">", e); } } } }; }
From source file:org.locationtech.geogig.repository.RepositoryInitializer.java
/** * Finds a {@code RepositoryInitializer} that {@link #canHandle(URI) can handle} the given URI, * or throws an {@code IllegalArgumentException} if no such initializer can be found. * <p>/*from w w w .j a va 2s . c o m*/ * The lookup method uses the standard JAVA SPI (Service Provider Interface) mechanism, by which * all the {@code META-INF/services/org.locationtech.geogig.repository.RepositoryInitializer} * files in the classpath will be scanned for fully qualified names of implementing classes. */ public static RepositoryInitializer lookup(URI repoURI) throws IllegalArgumentException { Preconditions.checkNotNull(repoURI, "Repository URI is null"); Iterator<RepositoryInitializer> initializers = ServiceLoader.load(RepositoryInitializer.class).iterator(); while (initializers.hasNext()) { RepositoryInitializer initializer = initializers.next(); if (initializer.canHandle(repoURI)) { return initializer; } } throw new IllegalArgumentException( "No repository initializer found capable of handling this kind of URI: " + repoURI); }
From source file:org.waveprotocol.box.server.persistence.protos.ProtoContactsDataSerializer.java
/** * Serialize {@link Contact} into {@link ProtoContact}. *///from ww w . ja v a2 s .c o m public static ProtoContacts serialize(List<Contact> contacts) { Preconditions.checkNotNull(contacts, "contacts is null"); ProtoContacts.Builder protoContacts = ProtoContacts.newBuilder(); for (Contact contact : contacts) { ProtoContacts.Contact.Builder protoContact = ProtoContacts.Contact.newBuilder(); protoContact.setParticipant(contact.getParticipantId().getAddress()); protoContact.setLastContactTime(contact.getLastContactTime()); protoContact.setScoreBonus(contact.getScoreBonus()); } return protoContacts.build(); }