@Override protected Long coerceToId(Object id) { try { if (id instanceof Long) { return (Long) id; } return Long.parseLong(id.toString()); } catch (NumberFormatException e) { Exceptions.ignore(e); return SQLEntity.NON_PERSISTENT_ENTITY_ID; } } }
/** * Waits the given amount of milliseconds. * <p> * If the given value is 0 or negative, the method returns immediately. If an <tt>InterruptedException</tt> * is thrown while waiting, the method will return immediately but ignore the exception. * * @param millisToWait the number of milliseconds to wait */ public static void millis(int millisToWait) { if (millisToWait > 0) { try { Thread.sleep(millisToWait); } catch (InterruptedException e) { Exceptions.ignore(e); Thread.currentThread().interrupt(); } } }
/** * Waits until all previously added promises completed or the given timeout expires. * * @param time the number of time intervals to wait for completion * @param unit the unit of time intervals to wait for completion * @return <tt>true</tt> if all promises completed within the given timout, <tt>false</tt> otherwise. */ public boolean await(long time, TimeUnit unit) { try { return semaphore.tryAcquire(promisesMade.get(), time, unit); } catch (InterruptedException e) { Thread.currentThread().interrupt(); Exceptions.ignore(e); return false; } } }
private String getETag(File file) { try { return com.google.common.io.Files.hash(file, Hashing.md5()).toString(); } catch (IOException e) { Exceptions.ignore(e); } return null; }
private boolean hasSetter(Field field) { try { field.getDeclaringClass().getMethod("set" + Reflection.toFirstUpper(field.getName()), field.getType()); return true; } catch (NoSuchMethodException e) { Exceptions.ignore(e); return false; } }
private static Method getterAsIs(@Nonnull Class<?> clazz, @Nonnull String property) throws NoSuchMethodException { try { return clazz.getMethod("is" + toFirstUpper(property)); } catch (NoSuchMethodException ex) { Exceptions.ignore(ex); return clazz.getMethod(property); } }
@Override @SuppressWarnings("unchecked") public Object transformFromSource(Object value) { List<Object> result = new ArrayList<>(); try { if (value instanceof List) { transformValues((List<?>) value, result); } } catch (IllegalArgumentException e) { Exceptions.ignore(e); } return result; }
private static void handleConfigError(String file, Exception e) { Exceptions.ignore(e); Sirius.LOG.WARN("Cannot load %s: %s", file, e.getMessage()); }
private static void doExec(boolean ignoreExitCodes, StringBuffer logger, Process p) throws ExecException { try { int code = p.waitFor(); if (code != 0 && !ignoreExitCodes) { Exception root = new Exception("Command returned with exit code " + code); throw new ExecException(root, logger.toString()); } } catch (InterruptedException e) { Exceptions.ignore(e); Thread.currentThread().interrupt(); } } }
/** * Returns an url encoded representation of the given <tt>value</tt> with <tt>UTF-8</tt> as character encoding. * * @param value the value to be encoded. * @return an url encoded representation of value, using UTF-8 as character encoding. */ public static String urlEncode(@Nullable String value) { if (isFilled(value)) { try { return URLEncoder.encode(value, Charsets.UTF_8.name()); } catch (UnsupportedEncodingException e) { // Cannot happen if Java-Version is > 1.4.... Exceptions.ignore(e); } } return value; }
private <T> T continueCoerceWithConversion(Class<T> targetClazz, T defaultValue) { if (data instanceof String) { try { return NLS.parseMachineString(targetClazz, data.toString().trim()); } catch (Exception e) { Exceptions.ignore(e); return defaultValue; } } throw new IllegalArgumentException(Strings.apply("Cannot convert '%s' to target class: %s ", data, targetClazz)); }
protected void safeClose() { for (BatchQuery<?> query : queries) { try { query.tryCommit(false); } catch (HandledException e) { Exceptions.ignore(e); } catch (Exception e) { Exceptions.handle(OMA.LOG, e); } query.safeClose(); } queries.clear(); connectionsPerRealm.values().forEach(this::safeCloseConnection); connectionsPerRealm.clear(); }
private static void invokeHandler(AccessPath accessPath, Method m, Object entity, Object... params) { try { m.setAccessible(true); if (m.getParameterCount() == 0) { m.invoke(accessPath.apply(entity)); } else { m.invoke(accessPath.apply(entity), params.length == 0 ? new Object[]{entity} : params); } } catch (IllegalAccessException ex) { throw Exceptions.handle(Mixing.LOG, ex); } catch (InvocationTargetException ex) { Exceptions.ignore(ex); throw Exceptions.handle(Mixing.LOG, ex.getTargetException()); } }
@SuppressWarnings({"unchecked", "rawtypes"}) private <T> T continueCoerceWithEnumTypes(Class<T> targetClazz, T defaultValue) { if (targetClazz.isEnum()) { try { if (Strings.isEmpty(asString(""))) { return defaultValue; } return (T) Enum.valueOf((Class<Enum>) targetClazz, asString("")); } catch (Exception e) { Exceptions.ignore(e); return (T) Enum.valueOf((Class<Enum>) targetClazz, asString("").toUpperCase()); } } return continueCoerceWithConversion(targetClazz, defaultValue); }
private static Double parseDecimalNumberFromUser(String value, String lang) { try { Double result = tryParseMachineFormat(value); if (result != null) { return result; } return getDecimalFormat(lang).parse(value).doubleValue(); } catch (ParseException e) { Exceptions.ignore(e); return Double.valueOf(value); } catch (NumberFormatException e) { throw new IllegalArgumentException(fmtr("NLS.errInvalidDecimalNumber").set("value", value).format(), e); } }
@Override @SuppressWarnings("unchecked") public Object transformFromSource(Object value) { try { return Value.of(value) .coerce((Class<Object>) getField().getType(), isNullAllowed() ? null : getField().getType().getEnumConstants()[0]); } catch (IllegalArgumentException e) { Exceptions.ignore(e); return isNullAllowed() ? null : getField().getType().getEnumConstants()[0]; } }
/** * Blocks the calling thread until the index is ready. */ public void waitForReady() { if (!isReady()) { try { Barrier b = Barrier.create(); b.add(readyFuture); b.await(); } catch (InterruptedException e) { Exceptions.ignore(e); Thread.currentThread().interrupt(); } } } }
/** * Closes the query by executing the last batch and releasing all resources. */ public void close() { if (stmt == null) { return; } try { commit(); } catch (HandledException ex) { Exceptions.ignore(ex); } safeClose(); context.unregister(this); }
@Override public long getMilliseconds(String path) { try { return configObject.toConfig().getDuration(path, TimeUnit.MILLISECONDS); } catch (ConfigException.Missing e) { Exceptions.ignore(e); return defaultConfig.toConfig().getDuration(path, TimeUnit.MILLISECONDS); } catch (Exception e) { throw Exceptions.handle(LOG, e); } }
private void waitForElasticsearchToBecomReady() { int retries = 15; while (retries-- > 0) { try { if (client.getRestClient().performRequest("GET", "/_cat/indices").getStatusLine().getStatusCode() == 200) { return; } } catch (Exception e) { Exceptions.ignore(e); } Elastic.LOG.INFO("Sleeping two seconds to wait until Elasticsearch is ready..."); Wait.seconds(2); } Elastic.LOG.WARN("Elasticsearch was not ready after waiting 30s..."); }