List of usage examples for java.util Objects requireNonNull
public static <T> T requireNonNull(T obj, Supplier<String> messageSupplier)
From source file:fr.landel.utils.commons.exception.ExceptionUtils.java
/** * Creates a supplier of the exception and injects the message * /*www . j a va 2 s . c o m*/ * <pre> * Optional.of(value).orElseThrow(ExceptionUtils.suppliesException(IllegalArgumentException::new, Locale.FRENCH, * "La valeur n'est pas comprise entre %,.3f et %,.3f", min, max)); * </pre> * * @param supplier * the exception supplier (required) * @param locale * the message locale (optional) * @param message * the error message (required) * @param arguments * the message arguments * @param <E> * the exception type * @return the exception supplier */ public static <E extends Throwable> Supplier<E> suppliesException(final Function<String, E> supplier, final Locale locale, final String message, final Object... arguments) { Objects.requireNonNull(supplier, "supplier"); Objects.requireNonNull(message, "message"); return () -> supplier.apply(String.format( ObjectUtils.defaultIfNull(locale, Locale.getDefault(Locale.Category.FORMAT)), message, arguments)); }
From source file:fr.landel.utils.commons.ObjectUtils.java
/** * Returns a default value if the object passed is {@code null}. * /*from w w w . j a v a 2 s .com*/ * <pre> * ObjectUtils.defaultIfNull(null, null, o -> o.getTitle()) = null * ObjectUtils.defaultIfNull(null, "", o -> o.getTitle()) = "" * ObjectUtils.defaultIfNull(null, "zz", o -> o.getTitle()) = "zz" * ObjectUtils.defaultIfNull("abc", "NO_TEXT", o -> o.toUpperCase()) = "ABC" * ObjectUtils.defaultIfNull("false", Boolean.TRUE, Boolean::parseBoolean) = Boolean.TRUE * </pre> * * @param object * the object to check * @param defaultObject * the default value * @param transformer * the object mapper * @param <T> * the type of the object to check * @param <X> * the type of the output * @return transformed {@code object} if not {@code null}, default value * otherwise * @throws NullPointerException * if {@code transformer} is {@code null} */ public static <T, X> X defaultIfNull(final T object, final X defaultObject, final Function<T, X> transformer) { Objects.requireNonNull(transformer, TRANSFORMER_ERROR); return object != null ? transformer.apply(object) : defaultObject; }
From source file:com.github.xbn.array.helper.ArrayHelperBaseComposer.java
public final void ciObjNullOrUnexpectedType_4prot(Object obj_supposedToBeArr, String array_objName) { Objects.requireNonNull(obj_supposedToBeArr, array_objName); ReflectRtxUtil.crashIfNotAssignableFrom(obj_supposedToBeArr.getClass(), getStaticClass()); }
From source file:com.cloudera.oryx.app.serving.kmeans.model.KMeansServingModelManager.java
/** * Called by the framework to initiate a continuous process of reading models, and reading * from the input topic and updating model state in memory, and issuing updates to the * update topic. This will be executed asynchronously and may block. * * @param updateIterator iterator to read models from * @throws IOException if an error occurs while reading updates *///from w ww.jav a 2 s . co m @Override public void consume(Iterator<KeyMessage<String, String>> updateIterator, Configuration hadoopConf) throws IOException { while (updateIterator.hasNext()) { KeyMessage<String, String> km = updateIterator.next(); String key = km.getKey(); String message = km.getMessage(); Objects.requireNonNull(key, "Bad message: " + km); switch (key) { case "UP": if (model == null) { continue; // No model to interpret with yet, so skip it } List<?> update = MAPPER.readValue(message, List.class); // Update int id = Integer.parseInt(update.get(0).toString()); double[] center = MAPPER.convertValue(update.get(1), double[].class); long count = Long.parseLong(update.get(2).toString()); model.update(id, center, count); break; case "MODEL": case "MODEL-REF": log.info("Loading new model"); PMML pmml = AppPMMLUtils.readPMMLFromUpdateKeyMessage(key, message, hadoopConf); KMeansPMMLUtils.validatePMMLVsSchema(pmml, inputSchema); List<ClusterInfo> clusters = KMeansPMMLUtils.read(pmml); model = new KMeansServingModel(clusters, inputSchema); log.info("New model: {}", model); break; default: throw new IllegalArgumentException("Bad message: " + km); } } }
From source file:com.github.yongchristophertang.engine.web.WebTemplateBuilder.java
/** * Set global default {@link ResultMatcher} for built {@link WebTemplate}. *///from w w w.ja va 2s. c o m public WebTemplateBuilder alwaysExpect(ResultMatcher resultMatcher) { Objects.requireNonNull(resultMatcher, "resultMatcher must not be null"); resultMatchers.add(resultMatcher); return this; }
From source file:de.tobiasbruns.fs20.sender.UsbService.java
private byte[] sendAndRecvData(UsbInterface iface, byte[] data) { Objects.requireNonNull(iface, "UsbInterface must not be null"); try {//from www .j a v a 2 s . c om forceClaim(iface); try { UsbPipe pipe = iface.getUsbEndpoint((byte) 0x01).getUsbPipe(); sendData(pipe, data); pipe = iface.getUsbEndpoint((byte) 0x81).getUsbPipe(); return reciveData(pipe); } finally { iface.release(); } } catch (UsbNotActiveException | UsbDisconnectedException | UsbException e) { throw new RuntimeException(e); } }
From source file:com.github.horrorho.inflatabledonkey.cloud.AuthorizeAssets.java
public AuthorizeAssets(String container, String zone) { this.container = Objects.requireNonNull(container, "container"); this.zone = Objects.requireNonNull(zone, "zone"); }
From source file:io.lavagna.web.security.PathConfiguration.java
List<UrlMatcher> buildMatcherList() { List<UrlMatcher> r = new ArrayList<>(); if (!loginUrlDisabled) { Objects.requireNonNull(loginUrlMatcher, "login urls must be configured or disabled"); Objects.requireNonNull(logoutUrlMatcher, "logout urls must be configured or disabled"); r.add(loginUrlMatcher);// w w w.j a v a 2s .c o m r.add(logoutUrlMatcher); } r.addAll(urlMatchers); return r; }
From source file:net.sf.jabref.model.entry.BibtexEntry.java
public BibtexEntry(String id, EntryType type) { Objects.requireNonNull(id, "Every BibtexEntry must have an ID"); this.id = id; setType(type); }
From source file:edu.tamu.tcat.oss.account.test.mock.MockDataSource.java
public void activate() { try {//from w w w . j a v a 2 s . com String url = props.getPropertyValue(PROP_URL, String.class); String user = props.getPropertyValue(PROP_USER, String.class); String pass = props.getPropertyValue(PROP_PASS, String.class); Objects.requireNonNull(url, "Database connection URL not supplied"); Objects.requireNonNull(user, "Database username not supplied"); Objects.requireNonNull(pass, "Database password not supplied"); int maxActive = getIntValue(props, PROP_MAX_ACTIVE, 30); int maxIdle = getIntValue(props, PROP_MAX_IDLE, 3); int minIdle = getIntValue(props, PROP_MIN_IDLE, 0); int minEviction = getIntValue(props, PROP_MIN_EVICTION, 10 * 1000); int betweenEviction = getIntValue(props, PROP_BETWEEN_EVICTION, 100); PostgreSqlDataSourceFactory factory = new PostgreSqlDataSourceFactory(); PostgreSqlPropertiesBuilder builder = factory.getPropertiesBuilder().create(url, user, pass); dataSource = factory.getDataSource(builder.getProperties()); //HACK: should add this API to the properties builder instead of downcasting and overriding { BasicDataSource basic = (BasicDataSource) dataSource; basic.setMaxActive(maxActive); basic.setMaxIdle(maxIdle); basic.setMinIdle(minIdle); basic.setMinEvictableIdleTimeMillis(minEviction); basic.setTimeBetweenEvictionRunsMillis(betweenEviction); } this.executor = Executors.newSingleThreadExecutor(); } catch (Exception e) { throw new IllegalStateException("Failed initializing data source", e); } }