Example usage for java.util Map isEmpty

List of usage examples for java.util Map isEmpty

Introduction

In this page you can find the example usage for java.util Map isEmpty.

Prototype

boolean isEmpty();

Source Link

Document

Returns true if this map contains no key-value mappings.

Usage

From source file:com.ksc.http.apache.utils.ApacheUtils.java

/**
 * Returns a new HttpClientContext used for request execution.
 *//*from w w  w.ja v a 2 s. c om*/
public static HttpClientContext newClientContext(HttpClientSettings settings,
        Map<String, ? extends Object> attributes) {
    final HttpClientContext clientContext = new HttpClientContext();

    if (attributes != null && !attributes.isEmpty()) {
        for (Map.Entry<String, ?> entry : attributes.entrySet()) {
            clientContext.setAttribute(entry.getKey(), entry.getValue());
        }
    }

    addPreemptiveAuthenticationProxy(clientContext, settings);
    return clientContext;

}

From source file:com.sunhz.projectutils.httputils.HttpClientUtils.java

/**
 * post access to the network, you need to use in the sub-thread
 *
 * @param url    url//from   ww w  .jav  a 2  s .  c  o  m
 * @param params params
 * @return response inputStream
 * @throws IOException In the case of non-200 status code is returned, it would have thrown
 */
public static InputStream postInputStream(String url, Map<String, String> params) throws IOException {
    List<NameValuePair> list = new ArrayList<NameValuePair>();
    if (params != null && !params.isEmpty()) {
        for (Map.Entry<String, String> entry : params.entrySet()) {
            list.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
        }
    }
    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list);
    HttpPost httpPost = new HttpPost(url);
    httpPost.setEntity(entity);
    org.apache.http.client.HttpClient client = new DefaultHttpClient();
    client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,
            Constant.TimeInApplication.NET_TIMEOUT);
    client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, Constant.TimeInApplication.NET_TIMEOUT);
    HttpResponse response = client.execute(httpPost);
    if (response.getStatusLine().getStatusCode() == 200) {
        return response.getEntity().getContent();
    } else {
        throw new IllegalArgumentException("postInputStreamInSubThread response status code is "
                + response.getStatusLine().getStatusCode());
    }

}

From source file:com.jayway.restassured.internal.print.RequestPrinter.java

private static void addMapDetails(StringBuilder builder, String title, Map<String, ?> map) {
    appendTab(builder.append(title));// www.ja v  a 2 s. c o m
    if (map.isEmpty()) {
        builder.append(NONE).append(NEW_LINE);
    } else {
        int i = 0;
        for (Map.Entry<String, ?> entry : map.entrySet()) {
            if (i++ != 0) {
                appendFourTabs(builder);
            }
            final Object value = entry.getValue();
            builder.append(entry.getKey());
            if (!(value instanceof NoParameterValue)) {
                builder.append(EQUALS).append(value);
            }
            builder.append(NEW_LINE);
        }
    }
}

From source file:com.bstek.dorado.util.Assert.java

/**
 * Map???message?<br>/*  w  w  w.jav  a 2  s . com*/
 * ?Map?nullsize0
 * @param map Map
 * @param message ??
 */
public static void notEmpty(Map<?, ?> map, String message) {
    if (map == null || map.isEmpty()) {
        throw new IllegalArgumentException(message);
    }
}

From source file:com.tealcube.minecraft.bukkit.mythicdrops.utils.SocketGemUtil.java

public static SocketGem getRandomSocketGemWithChance() {
    Map<String, SocketGem> socketGemMap = MythicDropsPlugin.getInstance().getSockettingSettings()
            .getSocketGemMap();//from   w w w. ja  va  2s. c om
    if (socketGemMap == null || socketGemMap.isEmpty()) {
        return null;
    }
    double totalWeight = 0;
    for (SocketGem sg : socketGemMap.values()) {
        totalWeight += sg.getChance();
    }

    double chosenWeight = MythicDropsPlugin.getInstance().getRandom().nextDouble() * totalWeight;

    double currentWeight = 0;

    List<SocketGem> l = new ArrayList<>(socketGemMap.values());
    Collections.shuffle(l);

    for (SocketGem sg : socketGemMap.values()) {
        currentWeight += sg.getChance();

        if (currentWeight >= chosenWeight) {
            return sg;
        }
    }
    return null;
}

From source file:com.datumbox.common.dataobjects.MatrixDataset.java

/**
 * Parses a dataset and converts it to MatrixDataset by using an already
 * existing mapping between feature names and column ids. Typically used
 * to parse the testing or validation dataset.
 * //from  w w w.  j  ava2  s. com
 * @param newDataset
 * @param featureIdsReference
 * @return 
 */
public static MatrixDataset parseDataset(Dataset newDataset, Map<Object, Integer> featureIdsReference) {
    if (featureIdsReference.isEmpty()) {
        throw new RuntimeException("The featureIdsReference map should not be empty.");
    }

    int n = newDataset.size();
    int d = featureIdsReference.size();

    MatrixDataset m = new MatrixDataset(new ArrayRealVector(n), new BlockRealMatrix(n, d), featureIdsReference);

    if (newDataset.isEmpty()) {
        return m;
    }

    boolean extractY = (Dataset
            .value2ColumnType(newDataset.iterator().next().getY()) == Dataset.ColumnType.NUMERICAL);

    boolean addConstantColumn = m.feature2ColumnId.containsKey(Dataset.constantColumnName);

    for (Record r : newDataset) {
        int row = r.getId();

        if (extractY) {
            m.Y.setEntry(row, Dataset.toDouble(r.getY()));
        }

        if (addConstantColumn) {
            m.X.setEntry(row, 0, 1.0); //add the constant column
        }
        for (Map.Entry<Object, Object> entry : r.getX().entrySet()) {
            Object feature = entry.getKey();
            Double value = Dataset.toDouble(entry.getValue());
            if (value != null) {
                Integer featureId = m.feature2ColumnId.get(feature);
                if (featureId != null) {//if the feature exists in our database
                    m.X.setEntry(row, featureId, value);
                }
            } else {
                //else the X matrix maintains the 0.0 default value
            }
        }
    }

    return m;
}

From source file:com.precioustech.fxtrading.utils.TradingUtils.java

/**
 * A utility method that is an alternative to having to write null and empty
 * checks for maps at various places in the code. Akin to
 * StringUtils.isEmpty() which returns true if input String is null or 0
 * length.// w w w . j  av a 2  s  . com
 * 
 * @param map
 * @return boolean true if map is null or is empty else false.
 * @see StringUtils#isEmpty(CharSequence)
 */
public static final boolean isEmpty(Map<?, ?> map) {
    return map == null || map.isEmpty();
}

From source file:com.greenline.hrs.admin.cache.redis.util.RedisInfoParser.java

/**
 * RedisInfoMapT?RedisInfoMapRedis?//from ww  w  .  ja va2 s.c  o  m
 * RedisServerInfoPO
 *
 * @param instanceType class
 * @param redisInfoMap Map
 * @param <T>          
 * @return instanceTypeinfomapnullinfoMap?instancenullRedisServerInfoPO
 */
public static <T> T parserRedisInfo(Class<T> instanceType, Map<String, String> redisInfoMap) {
    if (instanceType == null || redisInfoMap == null || redisInfoMap.isEmpty()) {
        return null;
    }
    T instance = null;
    try {
        instance = instanceType.newInstance();
    } catch (Exception e) {
        LOG.error("Instance:" + instanceType.getName(), e);
        return null;
    }
    Field[] fields = instanceType.getDeclaredFields();
    String inputName = null;
    for (Field field : fields) {
        ParserField parserField = field.getAnnotation(ParserField.class);
        if (parserField != null) {
            inputName = parserField.inputName();
        } else {
            inputName = field.getName();
        }
        if (redisInfoMap.containsKey(inputName)) {
            field.setAccessible(true);
            try {
                field.set(instance, ConvertUtils.convert(redisInfoMap.get(inputName), field.getType()));
            } catch (Exception e) {
                LOG.error("Field:" + field.getName() + "\t|\t value:" + redisInfoMap.get(inputName), e);
            }
        }
    }
    return instance;
}

From source file:org.keycloak.adapters.authentication.ClientCredentialsProviderUtils.java

public static ClientCredentialsProvider bootstrapClientAuthenticator(KeycloakDeployment deployment) {
    String clientId = deployment.getResourceName();
    Map<String, Object> clientCredentials = deployment.getResourceCredentials();

    String authenticatorId;/*from   ww  w . ja  v  a 2  s. c om*/
    if (clientCredentials == null || clientCredentials.isEmpty()) {
        authenticatorId = ClientIdAndSecretCredentialsProvider.PROVIDER_ID;
    } else {
        authenticatorId = (String) clientCredentials.get("provider");
        if (authenticatorId == null) {
            // If there is just one credential type, use provider from it
            if (clientCredentials.size() == 1) {
                authenticatorId = clientCredentials.keySet().iterator().next();
            } else {
                throw new RuntimeException(
                        "Can't identify clientAuthenticator from the configuration of client '" + clientId
                                + "' . Check your adapter configurations");
            }
        }
    }

    logger.debugf("Using provider '%s' for authentication of client '%s'", authenticatorId, clientId);

    Map<String, ClientCredentialsProvider> authenticators = new HashMap<>();
    loadAuthenticators(authenticators, ClientCredentialsProviderUtils.class.getClassLoader());
    loadAuthenticators(authenticators, Thread.currentThread().getContextClassLoader());

    ClientCredentialsProvider authenticator = authenticators.get(authenticatorId);
    if (authenticator == null) {
        throw new RuntimeException("Couldn't find ClientCredentialsProvider implementation class with id: "
                + authenticatorId + ". Loaded authentication providers: " + authenticators.keySet());
    }

    Object config = (clientCredentials == null) ? null : clientCredentials.get(authenticatorId);
    authenticator.init(deployment, config);

    return authenticator;
}

From source file:com.hubrick.vertx.s3.util.UrlEncodingUtils.java

public static String addParamsSortedToUrl(String url, Map<String, String> params) {
    checkNotNull(url, "url must not be null");
    checkNotNull(!url.isEmpty(), "url must not be empty");
    checkNotNull(params, "params must not be null");
    checkNotNull(!params.isEmpty(), "params must not be empty");

    final String baseUrl = extractBaseUrl(url);
    final String urlParams = baseUrl.equals(url) ? "" : url.replace(baseUrl + "?", "");
    final List<NameValuePair> nameValuePairs = URLEncodedUtils.parse(urlParams, Charsets.UTF_8);

    for (Map.Entry<String, String> paramToUrlEncode : params.entrySet().stream()
            .sorted(Comparator.comparing(Map.Entry::getKey)).collect(Collectors.toList())) {
        nameValuePairs.add(new BasicNameValuePair(paramToUrlEncode.getKey(), paramToUrlEncode.getValue()));
    }//ww w .  j  av  a 2s .  c  om

    return baseUrl + "?" + URLEncodedUtils.format(nameValuePairs, Charsets.UTF_8);
}