BandNameCreator { public String createUniqueAttributeName(String algorithmName, String sourceBandName) { final String desiredAttributeName = algorithmName + "_" + sourceBandName; if (mappedNames.containsKey(desiredAttributeName)) { return mappedNames.get(desiredAttributeName); } String attributeName = desiredAttributeName; final boolean tooLong = desiredAttributeName.length() > 10; if (tooLong) { attributeName = algorithmName + "_" + sourceBandName.replace("_", ""); attributeName = shorten(attributeName); } if (attributeName.length() > 10) { final int index = getIndex(algorithmName); attributeName = shorten(algorithmName) + "_" + index; if (attributeName.length() > 10) { final String indexPart = Integer.toString(index); final int idxLength = indexPart.length(); attributeName = attributeName.substring(0, 10 - idxLength - 1) + "_" + indexPart; } indexMap.put(algorithmName, index + 1); } if (tooLong) { BeamLogManager.getSystemLogger().warning( "attribute name '" + desiredAttributeName + "' exceeds 10 characters in length. Shortened to '" + attributeName + "'."); } addMapping(desiredAttributeName, attributeName); return attributeName; } BandNameCreator();  BandNameCreator(PrintStream printStream); String createUniqueAttributeName(String algorithmName, String sourceBandName);  }
@Test public void testCreateAttributeName() throws Exception { final int[] warningCount = new int[1]; final Handler handler = new Handler() { @Override public void publish(LogRecord record) { assertEquals(Level.WARNING, record.getLevel()); assertTrue(record.getMessage().contains("exceeds 10 characters in length. Shortened to")); warningCount[0]++; } @Override public void flush() { } @Override public void close() throws SecurityException { } }; final Level level = BeamLogManager.getSystemLogger().getLevel(); BeamLogManager.getSystemLogger().setLevel(Level.WARNING); BeamLogManager.getSystemLogger().addHandler(handler); BandNameCreator bandNameCreator = new BandNameCreator(); String attributeName1 = bandNameCreator.createUniqueAttributeName("median", "radiance_12"); String attributeName2 = bandNameCreator.createUniqueAttributeName("median", "radiance_13"); String attributeName3 = bandNameCreator.createUniqueAttributeName("p90_threshold", "radiance_12"); String attributeName4 = bandNameCreator.createUniqueAttributeName("p90_threshold", "radiance_13"); String attributeName5 = bandNameCreator.createUniqueAttributeName("p90_threshold", "algal2"); String attributeName6 = bandNameCreator.createUniqueAttributeName("p90_threshold", "algal1"); String attributeName7 = bandNameCreator.createUniqueAttributeName("maximum", "algal1"); String attributeName8 = bandNameCreator.createUniqueAttributeName("minimum", "algal1"); String attributeName9 = bandNameCreator.createUniqueAttributeName("median", "saharan_dust_index"); String attributeName10 = bandNameCreator.createUniqueAttributeName("median", "saharan_dust_index_normalized"); bandNameCreator.createUniqueAttributeName("median", "saharan_dust_index_a"); bandNameCreator.createUniqueAttributeName("median", "saharan_dust_index_b"); bandNameCreator.createUniqueAttributeName("median", "saharan_dust_index_c"); bandNameCreator.createUniqueAttributeName("median", "saharan_dust_index_d"); bandNameCreator.createUniqueAttributeName("median", "saharan_dust_index_e"); bandNameCreator.createUniqueAttributeName("median", "saharan_dust_index_f"); bandNameCreator.createUniqueAttributeName("median", "saharan_dust_index_g"); bandNameCreator.createUniqueAttributeName("median", "saharan_dust_index_h"); String attributeName11 = bandNameCreator.createUniqueAttributeName("median", "saharan_dust_index_xyz"); assertEquals("mdn_rdnc12", attributeName1); assertEquals("mdn_rdnc13", attributeName2); assertEquals("p90_thrs_0", attributeName3); assertEquals("p90_thrs_1", attributeName4); assertEquals("p90_thrs_2", attributeName5); assertEquals("p90_thrs_3", attributeName6); assertEquals("mx_lgl1", attributeName7); assertEquals("mn_lgl1", attributeName8); assertEquals("mdn_0", attributeName9); assertEquals("mdn_1", attributeName10); assertEquals("mdn_10", attributeName11); assertEquals(19, warningCount[0]); BeamLogManager.getSystemLogger().removeHandler(handler); BeamLogManager.getSystemLogger().setLevel(level); }
@Test public void testMapping() throws Exception { final StringBuilder stringBuilder = new StringBuilder(); final PrintStream stream = new PrintStream(new CsvStatisticsWriterTest.StringOutputStream(stringBuilder)); final BandNameCreator bandNameCreator = new BandNameCreator(stream); bandNameCreator.createUniqueAttributeName("median", "saharan_dust_index_a"); bandNameCreator.createUniqueAttributeName("median", "saharan_dust_index_b"); bandNameCreator.createUniqueAttributeName("maximum", "far_too_long_band_name"); bandNameCreator.createUniqueAttributeName("minimum", "far_too_long_band_name"); bandNameCreator.createUniqueAttributeName("total", "CHL"); assertEquals("mdn_0=median_saharan_dust_index_a\n" + "mdn_1=median_saharan_dust_index_b\n" + "mx_0=maximum_far_too_long_band_name\n" + "mn_0=minimum_far_too_long_band_name\n" + "total_CHL=total_CHL\n", stringBuilder.toString()); }