Example usage for java.util TreeMap get

List of usage examples for java.util TreeMap get

Introduction

In this page you can find the example usage for java.util TreeMap get.

Prototype

public V get(Object key) 

Source Link

Document

Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

Usage

From source file:org.apache.hconf2prop.Main.java

public static void convert(String arg) throws IOException {

    File input = new File(arg);
    if (!input.isFile() || !input.canRead()) {
        throw new IllegalArgumentException(arg);
    }//from  w  ww . j a va 2 s. co m

    Configuration conf = new Configuration();
    conf.addResource(new Path(input.toURI()));

    TreeMap<String, String> map = new TreeMap<String, String>();

    Iterator<Map.Entry<String, String>> entries = conf.iterator();
    while (entries.hasNext()) {
        Map.Entry<String, String> entry = entries.next();
        map.put(entry.getKey(), entry.getValue());
    }

    String base = FilenameUtils.getBaseName(input.getName());
    File output = new File(input.getParentFile(), base + ".prop");
    FileOutputStream stream = new FileOutputStream(output);
    Writer writer = new OutputStreamWriter(stream);
    BufferedWriter buffer = new BufferedWriter(writer);
    Iterator<String> keys = map.navigableKeySet().iterator();
    while (keys.hasNext()) {
        String key = keys.next();
        String value = map.get(key);
        String line = String.format("%s = %s", key, value);
        buffer.write(line);
        buffer.newLine();
    }
    buffer.close();

}

From source file:org.opendatakit.persistence.table.GrantedAuthorityHierarchyTable.java

public static final TreeMap<String, TreeSet<String>> getEntireGrantedAuthorityHierarchy(Datastore ds, User user)
        throws ODKDatastoreException {

    GrantedAuthorityHierarchyTable relation;
    List<? extends CommonFieldsBase> groupsList;
    relation = GrantedAuthorityHierarchyTable.assertRelation(ds, user);
    Query query = ds.createQuery(relation, "GrantedAuthorityHierarchyTable.getEntireGrantedAuthorityHierarchy",
            user);/*from w ww .  j a  v  a  2s . c o m*/
    query.addSort(GrantedAuthorityHierarchyTable.DOMINATING_GRANTED_AUTHORITY, Direction.ASCENDING);
    groupsList = query.executeQuery();

    TreeMap<String, TreeSet<String>> inheritFrom = new TreeMap<String, TreeSet<String>>();
    for (CommonFieldsBase b : groupsList) {
        GrantedAuthorityHierarchyTable group = (GrantedAuthorityHierarchyTable) b;

        GrantedAuthority dom = group.getDominatingGrantedAuthority();
        GrantedAuthority sub = group.getSubordinateGrantedAuthority();

        if (!GrantedAuthorityName.permissionsCanBeAssigned(dom.toString()))
            continue;
        TreeSet<String> auths = inheritFrom.get(dom.getAuthority());
        if (auths == null) {
            auths = new TreeSet<String>();
            inheritFrom.put(dom.getAuthority(), auths);
        }
        auths.add(sub.getAuthority());
    }

    return inheritFrom;
}

From source file:com.sshtools.common.vomanagementtool.common.VOHelper.java

public static boolean addNewVO(TreeMap info) throws IOException {
    boolean isSuccessful = false;
    String voname = (String) info.get("localvoname");
    String server = (String) info.get("server");
    int port = (Integer) info.get("port");
    String serverdn = (String) info.get("serverdn");
    String serverissuerdn = (String) info.get("serverissuerdn");
    //Make this directory in voms
    String path = vomsdir + File.separator + voname;

    if (!new File(path).isDirectory()) {
        if (new File(path).mkdir()) {
            try {
                Chmod(true, "go-w", path);
            } catch (IOException e) {
                throw new IOException("Cannot change directory permission of " + vomslocation + ".");
            }/* w ww  .j ava2s  .  co m*/
        }
    }
    //successfully created the directory
    //Now need to add a lsc file
    String lscfile = path + File.separator + server + ".lsc";
    if (!new File(lscfile).isFile()) {
        String content = serverdn + "\n" + serverissuerdn;
        try {
            writeToFile(lscfile, content);

            //Now add line to vomses file
            String lscString = "\"" + voname + "\"" + " \"" + server + "\"" + " \"" + port + "\"" + " \""
                    + serverdn + "\"" + " \"" + voname + "\"";
            try {
                appendLineToFavourites(lscString);
                isSuccessful = true;
            } catch (IOException e) {
                throw new IOException("Failed to append line\n" + lscString + "\nto " + voname + ".lsc file.");
            }
        } catch (IOException e) {
            throw new IOException("Failed to create " + voname + ".lsc file.");
        }
    } else {
        throw new IOException("VO '" + voname + "' with server '" + server + "' already exists.");
    }

    return isSuccessful;

}

From source file:com.sshtools.common.vomanagementtool.common.VOHelper.java

public static boolean addVOToFavourites(String voName, String fromGroup) {
    boolean isAdded = false;
    //First check if VO is already in Favourites
    TreeMap allFavVOs = getVOGroup(FAVOURITES);
    if (allFavVOs.containsKey(voName)) {
        JOptionPane.showMessageDialog(null, "VO '" + voName + "' is already in " + FAVOURITES + ".",
                "Error: Add VO to " + FAVOURITES, JOptionPane.ERROR_MESSAGE);
    } else {// w  w  w. j  av a2s.co  m
        TreeMap groupVOs = getVOGroup(fromGroup);
        if (groupVOs.containsKey(voName)) {
            List voDetails = (List) groupVOs.get(voName);
            allFavVOs.put(voName, voDetails);
            String newContent = createStringFromTreeMap(allFavVOs);
            try {
                writeToFile(favouritesFile, newContent);
                isAdded = true;
            } catch (IOException e) {
                JOptionPane.showMessageDialog(
                        null, "Cannot add VO '" + voName + "' to " + FAVOURITES + ". Write to file '"
                                + favouritesFile + "' failed",
                        "Error: Add VO to " + FAVOURITES, JOptionPane.ERROR_MESSAGE);
            }

        }
    }
    return isAdded;
}

From source file:api.Status.java

public static Result getStatus() {
    ObjectNode metrics = Json.newObject();

    OperatingSystemMXBean os = ManagementFactory.getOperatingSystemMXBean();
    TreeMap<String, Object> values = new TreeMap<String, Object>();
    for (Method method : os.getClass().getDeclaredMethods()) {
        method.setAccessible(true);/*from  w w w  .  j av  a  2s  .c o  m*/
        if (method.getName().startsWith("get") && Modifier.isPublic(method.getModifiers())) {
            Object value;
            try {
                value = method.invoke(os);
                values.put(method.getName(), value);
            } catch (Exception e) {
                Logger.warn("Error when invoking " + os.getClass().getName()
                        + " (OperatingSystemMXBean) method " + method.getName() + ": " + e);
            } // try
        } // if
    } // for

    metrics.put("jvmLoad", (Double) values.get("getProcessCpuLoad"));
    metrics.put("cpuLoad", (Double) values.get("getSystemCpuLoad"));
    metrics.put("openFD", (Long) values.get("getOpenFileDescriptorCount"));
    metrics.put("maxFD", (Long) values.get("getMaxFileDescriptorCount"));
    metrics.put("freeMemory", (Long) values.get("getFreePhysicalMemorySize"));
    metrics.put("totalMemory", (Long) values.get("getTotalPhysicalMemorySize"));

    return ok(metrics.toString());
}

From source file:org.commoncrawl.util.NodeAffinityMaskBuilder.java

public static String buildNodeAffinityMask(FileSystem fileSystem, Path partFileDirectory,
        Map<Integer, String> optionalRootMapHint, Set<String> excludedNodeList, int maxReducersPerNode,
        boolean skipBalance) throws IOException {

    TreeMap<Integer, String> partitionToNodeMap = new TreeMap<Integer, String>();
    FileStatus paths[] = fileSystem.globStatus(new Path(partFileDirectory, "part-*"));

    if (paths.length == 0) {
        throw new IOException("Invalid source Path:" + partFileDirectory);
    }/*from   w  ww  . j av a 2s.  c o m*/

    Multimap<String, Integer> inverseMap = TreeMultimap.create();
    Map<Integer, List<String>> paritionToDesiredCandidateList = new TreeMap<Integer, List<String>>();

    // iterate paths 
    for (FileStatus path : paths) {

        String currentFile = path.getPath().getName();
        int partitionNumber;
        try {
            if (currentFile.startsWith("part-r")) {
                partitionNumber = NUMBER_FORMAT.parse(currentFile.substring("part-r-".length())).intValue();
            } else {
                partitionNumber = NUMBER_FORMAT.parse(currentFile.substring("part-".length())).intValue();
            }
        } catch (ParseException e) {
            throw new IOException("Invalid Part Name Encountered:" + currentFile);
        }

        // get block locations 
        BlockLocation locations[] = fileSystem.getFileBlockLocations(path, 0, path.getLen());

        // if passed in root map is not null, then validate that all blocks for the current file reside on the desired node 
        if (optionalRootMapHint != null) {
            // the host all blocks should reside on 
            String desiredHost = optionalRootMapHint.get(partitionNumber);

            ArrayList<String> misplacedBlocks = new ArrayList<String>();
            // ok walk all blocks 
            for (BlockLocation location : locations) {
                boolean found = false;
                for (String host : location.getHosts()) {
                    if (host.compareTo(desiredHost) == 0) {
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    misplacedBlocks.add("Block At:" + location.getOffset() + " for File:" + path.getPath()
                            + " did not contain desired location:" + desiredHost);
                }

            }
            // ok pass test at a certain threshold 
            if (misplacedBlocks.size() != 0
                    && ((float) misplacedBlocks.size() / (float) locations.length) > .50f) {
                LOG.error("Misplaced Blocks Exceed Threshold");
                for (String misplacedBlock : misplacedBlocks) {
                    LOG.error(misplacedBlock);
                }
                // TODO: SKIP THIS STEP FOR NOW ??? 
                //throw new IOException("Misplaced Blocks Exceed Threshold!");
            }
            partitionToNodeMap.put(partitionNumber, desiredHost);
        } else {
            if (excludedNodeList != null) {
                // LOG.info("Exclued Node List is:" + Lists.newArrayList(excludedNodeList).toString());
            }
            // ok ask file system for block locations
            TreeMap<String, Integer> nodeToBlockCount = new TreeMap<String, Integer>();

            for (BlockLocation location : locations) {
                for (String host : location.getHosts()) {
                    if (excludedNodeList == null || !excludedNodeList.contains(host)) {
                        Integer nodeHitCount = nodeToBlockCount.get(host);
                        if (nodeHitCount == null) {
                            nodeToBlockCount.put(host, 1);
                        } else {
                            nodeToBlockCount.put(host, nodeHitCount.intValue() + 1);
                        }
                    }
                }
            }

            if (nodeToBlockCount.size() == 0) {
                throw new IOException("No valid nodes found for partition number:" + path);
            }

            Map.Entry<String, Integer> entries[] = nodeToBlockCount.entrySet().toArray(new Map.Entry[0]);
            Arrays.sort(entries, new Comparator<Map.Entry<String, Integer>>() {

                @Override
                public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
                    return o1.getValue().intValue() < o2.getValue().intValue() ? 1
                            : o1.getValue().intValue() == o2.getValue().intValue() ? 0 : -1;
                }
            });

            // build a list of nodes by priority ... 
            List<String> nodesByPriority = Lists.transform(Lists.newArrayList(entries),
                    new Function<Map.Entry<String, Integer>, String>() {

                        @Override
                        public String apply(Entry<String, Integer> entry) {
                            return entry.getKey();
                        }
                    });

            // stash it away ... 
            paritionToDesiredCandidateList.put(partitionNumber, nodesByPriority);
            //LOG.info("Mapping Partition:" + partitionNumber + " To Node:" + entries[0].getKey() + " BlockCount" + entries[0].getValue().intValue());
            partitionToNodeMap.put(partitionNumber, entries[0].getKey());
            // store the inverse mapping ... 
            inverseMap.put(entries[0].getKey(), partitionNumber);
        }
    }

    if (skipBalance) {
        // walk partition map to make sure everything is assigned ...
        /*
        for (String node : inverseMap.keys()) { 
          if (inverseMap.get(node).size() > maxReducersPerNode) { 
            throw new IOException("Node:" + node + " has too many partitions! ("+inverseMap.get(node).size());
          }
        }
        */
    }

    // now if optional root map hint is null 
    if (optionalRootMapHint == null && !skipBalance) {
        // figure out if there is an imbalance
        int avgRegionsPerNode = (int) Math.floor((float) paths.length / (float) inverseMap.keySet().size());
        int maxRegionsPerNode = (int) Math.ceil((float) paths.length / (float) inverseMap.keySet().size());
        LOG.info("Attempting to ideally balance nodes. Avg paritions per node:" + avgRegionsPerNode);

        // two passes .. 
        for (int pass = 0; pass < 2; ++pass) {
            LOG.info("Pass:" + pass);
            // iterate nodes ... 
            for (String node : ImmutableSet.copyOf(inverseMap.keySet())) {
                // get paritions in map  
                Collection<Integer> paritions = ImmutableList.copyOf(inverseMap.get(node));
                // if parition count exceeds desired average ... 
                if (paritions.size() > maxRegionsPerNode) {
                    // first pass, assign based on preference 
                    if (pass == 0) {
                        LOG.info("Node:" + node + " parition count:" + paritions.size() + " exceeds avg:"
                                + avgRegionsPerNode);
                        // walk partitions trying to find a node to discrard the parition to 
                        for (int partition : paritions) {
                            for (String candidate : paritionToDesiredCandidateList.get(partition)) {
                                if (!candidate.equals(node)) {
                                    // see if this candidate has room ..
                                    if (inverseMap.get(candidate).size() < avgRegionsPerNode) {
                                        LOG.info("REASSIGNING parition:" + partition + " from Node:" + node
                                                + " to Node:" + candidate);
                                        // found match reassign it ... 
                                        inverseMap.remove(node, partition);
                                        inverseMap.put(candidate, partition);
                                        break;
                                    }
                                }
                            }
                            // break out if reach our desired number of paritions for this node 
                            if (inverseMap.get(node).size() == avgRegionsPerNode)
                                break;
                        }
                    }
                    // second pass ... assign based on least loaded node ... 
                    else {
                        int desiredRelocations = paritions.size() - maxRegionsPerNode;
                        LOG.info("Desired Relocation for node:" + node + ":" + desiredRelocations
                                + " partitions:" + paritions.size());
                        for (int i = 0; i < desiredRelocations; ++i) {
                            String leastLoadedNode = null;
                            int leastLoadedNodePartitionCount = 0;

                            for (String candidateNode : inverseMap.keySet()) {
                                if (leastLoadedNode == null || inverseMap.get(candidateNode)
                                        .size() < leastLoadedNodePartitionCount) {
                                    leastLoadedNode = candidateNode;
                                    leastLoadedNodePartitionCount = inverseMap.get(candidateNode).size();
                                }
                            }
                            int bestPartition = -1;
                            int bestParitionOffset = -1;

                            for (int candidateParition : inverseMap.get(node)) {
                                int offset = 0;
                                for (String nodeCandidate : paritionToDesiredCandidateList
                                        .get(candidateParition)) {
                                    if (nodeCandidate.equals(leastLoadedNode)) {
                                        if (bestPartition == -1 || bestParitionOffset > offset) {
                                            bestPartition = candidateParition;
                                            bestParitionOffset = offset;
                                        }
                                        break;
                                    }
                                    offset++;
                                }
                            }
                            if (bestPartition == -1) {
                                bestPartition = Iterables.get(inverseMap.get(node), 0);
                            }
                            LOG.info("REASSIGNING parition:" + bestPartition + " from Node:" + node
                                    + " to Node:" + leastLoadedNode);
                            // found match reassign it ... 
                            inverseMap.remove(node, bestPartition);
                            inverseMap.put(leastLoadedNode, bestPartition);
                        }
                    }
                }
            }
        }
        LOG.info("Rebuilding parition to node map based on ideal balance");
        for (String node : inverseMap.keySet()) {
            LOG.info("Node:" + node + " has:" + inverseMap.get(node).size() + " partitions:"
                    + inverseMap.get(node).toString());
        }

        partitionToNodeMap.clear();
        for (Map.Entry<String, Integer> entry : inverseMap.entries()) {
            partitionToNodeMap.put(entry.getValue(), entry.getKey());
        }
    }

    StringBuilder builder = new StringBuilder();
    int itemCount = 0;
    for (Map.Entry<Integer, String> entry : partitionToNodeMap.entrySet()) {
        if (itemCount++ != 0)
            builder.append("\t");
        builder.append(entry.getKey().intValue() + "," + entry.getValue());
    }

    return builder.toString();
}

From source file:com.sfs.DataFilter.java

/**
 * Parses the text data into a collection.
 *
 * @param text the text//from ww  w  .  j  a v a  2  s .c  o m
 *
 * @return the collection< string>
 */
public static Collection<String> parseTextDataToCollection(final String text) {

    final Collection<String> textList = new ArrayList<String>();

    if (StringUtils.isNotBlank(text)) {
        TreeMap<Integer, TreeMap<Integer, String>> parsedMap = DataFilter.parseTextData(text);

        if (parsedMap != null) {
            for (Integer row : parsedMap.keySet()) {
                TreeMap<Integer, String> colMap = parsedMap.get(row);
                if (colMap != null) {
                    for (Integer col : colMap.keySet()) {
                        final String value = colMap.get(col);
                        if (StringUtils.isNotBlank(value)) {
                            textList.add(value.trim());
                        }
                    }
                }
            }
        }
    }
    return textList;
}

From source file:com.sun.faces.generate.GeneratorUtil.java

public static Map<String, ArrayList<RendererBean>> getComponentFamilyRendererMap(FacesConfigBean configBean,
        String renderKitId) {// www.  jav  a  2 s.c o m

    RenderKitBean renderKit = configBean.getRenderKit(renderKitId);
    if (renderKit == null) {
        throw new IllegalArgumentException("No RenderKit for id '" + renderKitId + '\'');
    }

    RendererBean[] renderers = renderKit.getRenderers();
    if (renderers == null) {
        throw new IllegalStateException("No Renderers for RenderKit id" + '"' + renderKitId + '"');
    }

    TreeMap<String, ArrayList<RendererBean>> result = new TreeMap<String, ArrayList<RendererBean>>();

    for (int i = 0, len = renderers.length; i < len; i++) {
        RendererBean renderer = renderers[i];

        if (renderer == null) {
            throw new IllegalStateException("no Renderer");
        }

        // if this is the first time we've encountered this
        // componentFamily
        String componentFamily = renderer.getComponentFamily();
        ArrayList<RendererBean> list = result.get(componentFamily);
        if (list == null) {
            // create a list for it
            list = new ArrayList<RendererBean>();
            list.add(renderer);
            result.put(componentFamily, list);
        } else {
            list.add(renderer);
        }
    }

    return result;

}

From source file:org.apache.hadoop.hbase.master.procedure.MasterDDLOperationHelper.java

/**
 * Reopen all regions from a table after a schema change operation.
 **//*from   w w w  .  ja  v  a2  s.  co m*/
public static boolean reOpenAllRegions(final MasterProcedureEnv env, final TableName tableName,
        final List<HRegionInfo> regionInfoList) throws IOException {
    boolean done = false;
    LOG.info("Bucketing regions by region server...");
    List<HRegionLocation> regionLocations = null;
    Connection connection = env.getMasterServices().getConnection();
    try (RegionLocator locator = connection.getRegionLocator(tableName)) {
        regionLocations = locator.getAllRegionLocations();
    }
    // Convert List<HRegionLocation> to Map<HRegionInfo, ServerName>.
    NavigableMap<HRegionInfo, ServerName> hri2Sn = new TreeMap<HRegionInfo, ServerName>();
    for (HRegionLocation location : regionLocations) {
        hri2Sn.put(location.getRegionInfo(), location.getServerName());
    }
    TreeMap<ServerName, List<HRegionInfo>> serverToRegions = Maps.newTreeMap();
    List<HRegionInfo> reRegions = new ArrayList<HRegionInfo>();
    for (HRegionInfo hri : regionInfoList) {
        ServerName sn = hri2Sn.get(hri);
        // Skip the offlined split parent region
        // See HBASE-4578 for more information.
        if (null == sn) {
            LOG.info("Skip " + hri);
            continue;
        }
        if (!serverToRegions.containsKey(sn)) {
            LinkedList<HRegionInfo> hriList = Lists.newLinkedList();
            serverToRegions.put(sn, hriList);
        }
        reRegions.add(hri);
        serverToRegions.get(sn).add(hri);
    }

    LOG.info("Reopening " + reRegions.size() + " regions on " + serverToRegions.size() + " region servers.");
    AssignmentManager am = env.getMasterServices().getAssignmentManager();
    am.setRegionsToReopen(reRegions);
    BulkReOpen bulkReopen = new BulkReOpen(env.getMasterServices(), serverToRegions, am);
    while (true) {
        try {
            if (bulkReopen.bulkReOpen()) {
                done = true;
                break;
            } else {
                LOG.warn("Timeout before reopening all regions");
            }
        } catch (InterruptedException e) {
            LOG.warn("Reopen was interrupted");
            // Preserve the interrupt.
            Thread.currentThread().interrupt();
            break;
        }
    }
    return done;
}

From source file:org.apache.hadoop.hive.ql.exec.ToolBox.java

static <T> ArrayList<TreeMap<String, T>> aggregateKey(TreeMap<String, T> para, String delimiter, int idx) {
    ArrayList<TreeMap<String, T>> a = new ArrayList<TreeMap<String, T>>();
    String prekey = null;/*  w  w  w .  j av a 2  s.c o m*/
    TreeMap<String, T> h = null;
    for (String s : para.keySet()) {
        if (prekey == null) {
            prekey = retrieveComponent(s, delimiter, idx);
            h = new TreeMap<String, T>();
            h.put(s, para.get(s));
        } else if (prekey.equals(s)) {
            h.put(s, para.get(s));
        } else {
            prekey = retrieveComponent(s, delimiter, idx);
            ;
            a.add(h);
            h = new TreeMap<String, T>();
            h.put(s, para.get(s));
        }

    }
    a.add(h);
    return a;
}