Example usage for java.util Collections addAll

List of usage examples for java.util Collections addAll

Introduction

In this page you can find the example usage for java.util Collections addAll.

Prototype

@SafeVarargs
public static <T> boolean addAll(Collection<? super T> c, T... elements) 

Source Link

Document

Adds all of the specified elements to the specified collection.

Usage

From source file:org.dasein.cloud.cloudstack.network.LoadBalancers.java

private void toRule(@Nullable Node node, @Nonnull Map<String, LoadBalancer> current)
        throws InternalException, CloudException {
    NodeList attributes = node.getChildNodes();
    int publicPort = -1, privatePort = -1;
    LbAlgorithm algorithm = null;/*from w w  w .  j  a  v  a 2 s  . co  m*/
    String publicIp = null;
    String vlanId = null;
    String ruleId = null;
    String lbName = null;
    String lbDesc = ""; // can't be null

    for (int i = 0; i < attributes.getLength(); i++) {
        Node n = attributes.item(i);
        String name = n.getNodeName().toLowerCase();
        String value;

        if (n.getChildNodes().getLength() > 0) {
            value = n.getFirstChild().getNodeValue();
        } else {
            value = null;
        }
        if (name.equals("publicip")) {
            publicIp = value;
        } else if (name.equals("networkid")) {
            vlanId = value;
        } else if (name.equals("id")) {
            ruleId = value;
        } else if (name.equals("publicport") && value != null) {
            publicPort = Integer.parseInt(value);
        } else if (name.equals("privateport") && value != null) {
            privatePort = Integer.parseInt(value);
        } else if (name.equals("algorithm")) {
            if (value == null || value.equals("roundrobin")) {
                algorithm = LbAlgorithm.ROUND_ROBIN;
            } else if (value.equals("leastconn")) {
                algorithm = LbAlgorithm.LEAST_CONN;
            } else if (value.equals("")) {
                algorithm = LbAlgorithm.SOURCE;
            } else {
                algorithm = LbAlgorithm.ROUND_ROBIN;
            }
        } else if (name.equals("name")) {
            lbName = value;
        } else if (name.equals("description")) {
            lbDesc = value;
        }
    }
    LbListener listener = LbListener.getInstance(algorithm, LbPersistence.NONE, LbProtocol.RAW_TCP, publicPort,
            privatePort);
    Collection<String> serverIds = getServersAt(ruleId);

    if (current.containsKey(publicIp)) {
        LoadBalancer lb = current.get(publicIp);

        @SuppressWarnings("deprecation")
        String[] currentIds = lb.getProviderServerIds();
        LbListener[] listeners = lb.getListeners();

        // TODO: WTF?
        Set<Integer> ports = new TreeSet<Integer>();

        for (int port : lb.getPublicPorts()) {
            ports.add(port);
        }
        ports.add(publicPort);

        int[] portList = new int[ports.size()];
        int i = 0;

        for (Integer p : ports) {
            portList[i++] = p;
        }
        //noinspection deprecation
        lb.setPublicPorts(portList);

        boolean there = false;

        for (LbListener l : listeners) {
            if (l.getAlgorithm().equals(listener.getAlgorithm())) {
                if (l.getNetworkProtocol().equals(listener.getNetworkProtocol())) {
                    if (l.getPublicPort() == listener.getPublicPort()) {
                        if (l.getPrivatePort() == listener.getPrivatePort()) {
                            there = true;
                            break;
                        }
                    }
                }
            }
        }
        if (!there) {
            lb.withListeners(listener);
        }
        // TODO: WTF?
        TreeSet<String> newIds = new TreeSet<String>();

        Collections.addAll(newIds, currentIds);
        for (String id : serverIds) {
            newIds.add(id);
        }
        //noinspection deprecation
        lb.setProviderServerIds(newIds.toArray(new String[newIds.size()]));
        //noinspection deprecation
        lb.setName(lbName);
        //noinspection deprecation
        lb.setDescription(lbDesc);
    } else {
        Collection<DataCenter> dcs = getProvider().getDataCenterServices()
                .listDataCenters(getProvider().getContext().getRegionId());
        String[] ids = new String[dcs.size()];
        int i = 0;

        for (DataCenter dc : dcs) {
            ids[i++] = dc.getProviderDataCenterId();
        }

        LoadBalancer lb = LoadBalancer.getInstance(getContext().getAccountNumber(), getContext().getRegionId(),
                publicIp, LoadBalancerState.ACTIVE, lbName, lbDesc, LoadBalancerAddressType.IP, publicIp,
                publicPort).withListeners(listener).operatingIn(ids);
        lb.forVlan(vlanId);
        //noinspection deprecation
        lb.setProviderServerIds(serverIds.toArray(new String[serverIds.size()]));
        current.put(publicIp, lb);
    }
}

From source file:com.connectsdk.service.CastService.java

@Override
protected void updateCapabilities() {
    List<String> capabilities = new ArrayList<String>();

    Collections.addAll(capabilities, MediaPlayer.Capabilities);
    Collections.addAll(capabilities, VolumeControl.Capabilities);

    capabilities.add(Play);// w w  w.j av a2 s  .c  o m
    capabilities.add(Pause);
    capabilities.add(Stop);
    capabilities.add(Duration);
    capabilities.add(Seek);
    capabilities.add(Position);
    capabilities.add(PlayState);
    capabilities.add(Subtitles_Vtt);
    capabilities.add(PlayState_Subscribe);

    capabilities.add(WebAppLauncher.Launch);
    capabilities.add(Message_Send);
    capabilities.add(Message_Receive);
    capabilities.add(Message_Send_JSON);
    capabilities.add(Message_Receive_JSON);
    capabilities.add(WebAppLauncher.Connect);
    capabilities.add(WebAppLauncher.Disconnect);
    capabilities.add(WebAppLauncher.Join);
    capabilities.add(WebAppLauncher.Close);

    setCapabilities(capabilities);
}

From source file:com.mawujun.util.ObjectUtils.java

/**
 * Find the "best guess" middle value among comparables. If there is an even
 * number of total values, the lower of the two middle values will be returned.
 * @param <T> type of values processed by this method
 * @param items to compare/*  w  w w  .  java2 s  .co m*/
 * @return T at middle position
 * @throws NullPointerException if items is {@code null}
 * @throws IllegalArgumentException if items is empty or contains {@code null} values
 * @since 3.0.1
 */
public static <T extends Comparable<? super T>> T median(T... items) {
    Validate.notEmpty(items);
    Validate.noNullElements(items);
    TreeSet<T> sort = new TreeSet<T>();
    Collections.addAll(sort, items);
    @SuppressWarnings("unchecked") //we know all items added were T instances
    T result = (T) sort.toArray()[(sort.size() - 1) / 2];
    return result;
}