Search in sources :

Example 41 with SortedMap

use of java.util.SortedMap in project OpenClinica by OpenClinica.

the class SpreadsheetPreviewNw method createItemsOrSectionMap.

/**
     * This method searches for a sheet named "Items" or "Sections" in an Excel Spreadsheet object, then creates a sorted Map whose members represent a row of
     * data for each "Item" or "Section" on the sheet. This method was created primarily to get Items and section data for previewing a CRF.
     *
     * @return A SortedMap implementation (TreeMap) containing row numbers, each pointing to a Map. The Maps represent each Item or section row in a
     *         spreadsheet. The items or sections themselves are in rows 1..N. An example data value from a Section row is: 1: {page_number=1.0,
     *         section_label=Subject Information, section_title=SimpleSection1} Returns an empty Map if the spreadsheet does not contain any sheets named
     *         "sections" or "items" (case insensitive).
     * @param workbook
     *            is an object representing a spreadsheet.
     * @param itemsOrSection
     *            should specify "items" or "sections" or the associated static variable, i.e. SpreadsheetPreview.ITEMS
     */
public Map<Integer, Map<String, String>> createItemsOrSectionMap(HSSFWorkbook workbook, String itemsOrSection) {
    if (workbook == null || workbook.getNumberOfSheets() == 0) {
        return new HashMap<Integer, Map<String, String>>();
    }
    if (itemsOrSection == null || !itemsOrSection.equalsIgnoreCase(ITEMS) && !itemsOrSection.equalsIgnoreCase(SECTIONS)) {
        return new HashMap<Integer, Map<String, String>>();
    }
    HSSFSheet sheet;
    HSSFRow row;
    HSSFCell cell;
    // static item headers for a CRF; TODO: change these so they are not
    // static and hard-coded
    /*
         * New itemHeaders String[] itemHeaders = {"item_name","description_label","left_item_text",
         * "units","right_item_text","section_label","group_label","header", "subheader","parent_item","column_number","page_number",
         * "question_number","response_type","response_label", "response_options_text","response_values","response_layout","default_value", "data_type",
         * "validation","validation_error_message","phi","required"};
         */
    String[] itemHeaders = { "item_name", "description_label", "left_item_text", "units", "right_item_text", "section_label", "group_label", "header", "subheader", "parent_item", "column_number", "page_number", "question_number", "response_type", "response_label", "response_options_text", "response_values", "response_layout", "default_value", "data_type", "width_decimal", "validation", "validation_error_message", "phi", "required" };
    String[] sectionHeaders = { "section_label", "section_title", "subtitle", "instructions", "page_number", "parent_section", "borders" };
    Map<String, String> rowCells = new HashMap<String, String>();
    SortedMap<Integer, Map<String, String>> allRows = new TreeMap<Integer, Map<String, String>>();
    String str;
    String dateFormat = "";
    for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
        sheet = workbook.getSheetAt(i);
        str = workbook.getSheetName(i);
        if (str.equalsIgnoreCase(itemsOrSection)) {
            for (int j = 1; j < sheet.getPhysicalNumberOfRows(); j++) {
                String[] headers = itemsOrSection.equalsIgnoreCase(ITEMS) ? itemHeaders : sectionHeaders;
                // time again.
                if (j > 1)
                    rowCells = new HashMap<String, String>();
                row = sheet.getRow(j);
                if (row == null) {
                    continue;
                }
                for (int k = 0; k < headers.length; k++) {
                    cell = row.getCell((short) k);
                    if ("default_value".equalsIgnoreCase(headers[k]) && isDateDatatype(headers, row) && !"".equalsIgnoreCase(getCellValue(cell))) {
                        try {
                            // BWP>> getDateCellValue() wll throw an
                            // exception if
                            // the value is an invalid date. Keep the date
                            // format the same as
                            // it is in the database; MM/dd/yyyy
                            // String pttrn = ResourceBundleProvider.getFormatBundle().getString("oc_date_format_string");
                            String pttrn = ApplicationConstants.getDateFormatInItemData();
                            dateFormat = new SimpleDateFormat(pttrn).format(cell.getDateCellValue());
                            rowCells.put(headers[k], dateFormat);
                            continue;
                        } catch (Exception e) {
                            String cellVal = getCellValue(cell);
                            logger.info("An invalid date format was encountered when reading a default value in the spreadsheet.");
                            rowCells.put(headers[k], cellVal);
                            continue;
                        }
                    }
                    if (headers[k].equalsIgnoreCase("left_item_text") || headers[k].equalsIgnoreCase("right_item_text") || headers[k].equalsIgnoreCase("header") || headers[k].equalsIgnoreCase("subheader") || headers[k].equalsIgnoreCase("question_number") || headers[k].equalsIgnoreCase("section_title") || headers[k].equalsIgnoreCase("subtitle") || headers[k].equalsIgnoreCase("instructions") || headers[k].equalsIgnoreCase("response_options_text")) {
                        rowCells.put(headers[k], getCellValue(cell).replaceAll("\\\\,", "\\,"));
                    } else {
                        rowCells.put(headers[k], getCellValue(cell).replaceAll("\\\\,", "\\,").replaceAll("<[^>]*>", ""));
                    }
                //logger.warn("BADS: "+headers[k]+": "+getCellValue(cell));
                }
                // item_name
                allRows.put(j, rowCells);
            }
        // end inner for loop
        }
    // end if
    }
    // end outer for
    return allRows;
}
Also used : HashMap(java.util.HashMap) HSSFRow(org.apache.poi.hssf.usermodel.HSSFRow) TreeMap(java.util.TreeMap) IOException(java.io.IOException) HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map) SortedMap(java.util.SortedMap) SimpleDateFormat(java.text.SimpleDateFormat)

Example 42 with SortedMap

use of java.util.SortedMap in project OpenClinica by OpenClinica.

the class SpreadsheetPreviewNw method createGroupsMap.

public Map<Integer, Map<String, String>> createGroupsMap(HSSFWorkbook workbook) {
    if (workbook == null || workbook.getNumberOfSheets() == 0) {
        return new HashMap<Integer, Map<String, String>>();
    }
    HSSFSheet sheet;
    HSSFRow row;
    HSSFCell cell;
    sheet = workbook.getSheetAt(4);
    cell = sheet.getRow(1).getCell((short) 0);
    String version = cell.getStringCellValue();
    // static group headers for a CRF; TODO: change these so they are not
    // static and hard-coded
    // BWP>>remove "group_borders" column
    String[] groupHeaders = { "group_label", "repeating_group", "group_header", "group_repeat_number", "group_repeat_max" };
    if (version.equalsIgnoreCase("Version: 2.2") || version.equalsIgnoreCase("Version: 2.5") || version.equalsIgnoreCase("Version: 3.0")) {
        groupHeaders = new String[] { "group_label", "group_header", "group_repeat_number", "group_repeat_max" };
    }
    Map<String, String> rowCells = new HashMap<String, String>();
    SortedMap<Integer, Map<String, String>> allRows = new TreeMap<Integer, Map<String, String>>();
    String str;
    for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
        sheet = workbook.getSheetAt(i);
        str = workbook.getSheetName(i);
        if (str.equalsIgnoreCase("Groups")) {
            for (int j = 1; j < sheet.getPhysicalNumberOfRows(); j++) {
                // time again.
                if (j > 1)
                    rowCells = new HashMap<String, String>();
                row = sheet.getRow(j);
                for (int k = 0; k < groupHeaders.length; k++) {
                    cell = row.getCell((short) k);
                    if (groupHeaders[k].equalsIgnoreCase("group_header")) {
                        rowCells.put(groupHeaders[k], getCellValue(cell));
                    } else {
                        rowCells.put(groupHeaders[k], getCellValue(cell).replaceAll("<[^>]*>", ""));
                    }
                }
                allRows.put(j, rowCells);
            }
        // end inner for loop
        }
    // end if
    }
    // end outer for
    return allRows;
}
Also used : HashMap(java.util.HashMap) HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell) HSSFRow(org.apache.poi.hssf.usermodel.HSSFRow) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map) SortedMap(java.util.SortedMap)

Example 43 with SortedMap

use of java.util.SortedMap in project alluxio by Alluxio.

the class AlluxioMasterRestServiceHandlerTest method getMetrics.

@Test
public void getMetrics() {
    final int FILES_PINNED_TEST_VALUE = 100;
    String filesPinnedProperty = MetricsSystem.getMasterMetricName(FileSystemMaster.Metrics.FILES_PINNED);
    Gauge<Integer> filesPinnedGauge = new Gauge<Integer>() {

        @Override
        public Integer getValue() {
            return FILES_PINNED_TEST_VALUE;
        }
    };
    MetricSet mockMetricsSet = mock(MetricSet.class);
    Map<String, Metric> map = new HashMap<>();
    map.put(filesPinnedProperty, filesPinnedGauge);
    when(mockMetricsSet.getMetrics()).thenReturn(map);
    MetricsSystem.METRIC_REGISTRY.registerAll(mockMetricsSet);
    Response response = mHandler.getMetrics();
    try {
        assertNotNull("Response must be not null!", response);
        assertNotNull("Response must have a entry!", response.getEntity());
        assertTrue("Entry must be a SortedMap!", (response.getEntity() instanceof SortedMap));
        SortedMap<String, Long> metricsMap = (SortedMap<String, Long>) response.getEntity();
        assertFalse("Metrics Map must be not empty!", (metricsMap.isEmpty()));
        assertTrue("Map must contain key " + filesPinnedProperty + "!", metricsMap.containsKey(filesPinnedProperty));
        assertEquals(FILES_PINNED_TEST_VALUE, metricsMap.get(filesPinnedProperty).longValue());
    } finally {
        response.close();
    }
}
Also used : HashMap(java.util.HashMap) Matchers.anyString(org.mockito.Matchers.anyString) Gauge(com.codahale.metrics.Gauge) MetricSet(com.codahale.metrics.MetricSet) Response(javax.ws.rs.core.Response) SortedMap(java.util.SortedMap) Metric(com.codahale.metrics.Metric) Test(org.junit.Test)

Example 44 with SortedMap

use of java.util.SortedMap in project opennms by OpenNMS.

the class NotificationWizardServlet method updatePaths.

private void updatePaths(final String rule, final String criticalIp, final String criticalSvc) throws FilterParseException, SQLException {
    final Connection conn = DataSourceFactory.getInstance().getConnection();
    final DBUtils d = new DBUtils(getClass(), conn);
    try {
        final SortedMap<Integer, String> nodes = getFilterDao().getNodeMap(rule);
        for (final Map.Entry<Integer, String> entry : nodes.entrySet()) {
            final int key = entry.getKey().intValue();
            deleteCriticalPath(key, conn);
            if (criticalIp != null && !"".equals(criticalIp)) {
                setCriticalPath(key, criticalIp, criticalSvc, conn);
            }
        }
    } finally {
        d.cleanUp();
    }
}
Also used : Connection(java.sql.Connection) DBUtils(org.opennms.core.utils.DBUtils) HashMap(java.util.HashMap) Map(java.util.Map) SortedMap(java.util.SortedMap)

Example 45 with SortedMap

use of java.util.SortedMap in project graylog2-server by Graylog2.

the class ThroughputCalculator method doRun.

@Override
public void doRun() {
    final SortedMap<String, ? extends Counting> counters = metricRegistry.getCounters(filterSingleMetric(GlobalMetricNames.OUTPUT_THROUGHPUT));
    // rinse and repeat for input throughput
    final SortedMap<String, ? extends Counting> inputCounters = metricRegistry.getCounters(filterSingleMetric(GlobalMetricNames.INPUT_THROUGHPUT));
    // StreamMetrics isn't accessible here, so we need to use a metrics filter instead.
    final SortedMap<String, ? extends Counting> streamMeters = metricRegistry.getMeters(streamMetricFilter);
    final Iterable<Map.Entry<String, ? extends Counting>> entries = Iterables.concat(counters.entrySet(), inputCounters.entrySet(), streamMeters.entrySet());
    // calculate rates
    for (Map.Entry<String, ? extends Counting> countingEntry : entries) {
        final Counting value = countingEntry.getValue();
        final String metricName = countingEntry.getKey();
        CounterSample counterSample = sampledCounters.get(metricName);
        if (counterSample == null) {
            counterSample = new CounterSample();
            sampledCounters.put(metricName, counterSample);
        }
        counterSample.updateAverage(value.getCount());
        final String rateName = name(metricName, GlobalMetricNames.RATE_SUFFIX);
        if (!metricRegistry.getMetrics().containsKey(rateName)) {
            try {
                log.debug("Registering derived, per-second metric {}", rateName);
                metricRegistry.register(rateName, new Gauge<Double>() {

                    @Override
                    public Double getValue() {
                        final CounterSample sample = sampledCounters.get(metricName);
                        return sample == null ? 0d : sample.getCurrentAverage();
                    }
                });
            } catch (IllegalArgumentException e) {
                log.warn("Could not register gauge {} despite checking before that it didn't exist. This should not happen.", rateName);
            }
        }
    }
}
Also used : ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) SortedMap(java.util.SortedMap) Counting(com.codahale.metrics.Counting)

Aggregations

SortedMap (java.util.SortedMap)228 Map (java.util.Map)174 TreeMap (java.util.TreeMap)115 HashMap (java.util.HashMap)66 NavigableMap (java.util.NavigableMap)33 ArrayList (java.util.ArrayList)31 Iterator (java.util.Iterator)31 Test (org.junit.Test)25 ImmutableMap (com.google.common.collect.ImmutableMap)21 IOException (java.io.IOException)20 List (java.util.List)17 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)17 JASIExpr (org.matheclipse.core.convert.JASIExpr)16 IExpr (org.matheclipse.core.interfaces.IExpr)16 ImmutableSortedMap (com.google.common.collect.ImmutableSortedMap)14 File (java.io.File)14 Entry (java.util.Map.Entry)13 ConcurrentMap (java.util.concurrent.ConcurrentMap)12 ConcurrentNavigableMap (java.util.concurrent.ConcurrentNavigableMap)12 LinkedHashMap (java.util.LinkedHashMap)11