Java Code Examples for java.util.Map.compute()

The following are Jave code examples for showing how to use compute() of the java.util.Map class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: MDC-Discord-Bot   File: AutomodListener.java   View Source Code Vote up 6 votes
private boolean checkSpamCharacters(String channelName, String content)
{
    if(!Config.getContains(EConfigs.CHANNEL_SPAM_CHARACTERS_BLACKLIST, channelName)) return false;
    if(content.length() <= 10) return false;

    //Count all the characters in the message
    Map<Character, Integer> characterCount = new HashMap<>();
    for(char c : content.toCharArray())
        characterCount.compute(c, (character, integer) -> integer == null ? 1 : integer + 1);

    //Add up all of the major repeated characters
    int spamCharCount = 0;
    for(int charCount : characterCount.values())
        if(charCount > (int) Math.ceil((float) content.length() * 0.2f))
            spamCharCount += charCount;

    return spamCharCount > (int) Math.ceil((float) content.length() * 0.6f);
}
 
Example 2
Project: JInsight   File: LogbackInstrumentationTest.java   View Source Code Vote up 6 votes
@Test
public void testLogLevel() throws Exception {
  logger.setLevel(Level.ERROR);

  Map<String, Long> expectedCounts = getCurrentCounts();
  expectedCounts.compute("total", (s, aLong) -> aLong + 1);
  expectedCounts.compute("error", (s, aLong) -> aLong + 1);

  logger.trace("trace!");
  logger.debug("debug!");
  logger.info("info!");
  logger.warn("warn!");
  logger.error("error!");

  assertEquals(expectedCounts, getCurrentCounts());
}
 
Example 3
Project: hack   File: LongestHarmoniousSubsequence.java   View Source Code Vote up 6 votes
public int findLHS(int[] nums) {
    int result = 0;

    Map<Integer, Integer> map = new HashMap<>();

    for (int num : nums) {
        map.compute(num, (key, value) -> value == null ? 1 : value + 1);
    }

    for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
        if (map.containsKey(entry.getKey() + 1)) {
            result = Math.max(result, entry.getValue() + map.get(entry.getKey() + 1));
        }
    }

    return result;
}
 
Example 4
Project: JInsight   File: Log4J2InstrumentationTest.java   View Source Code Vote up 6 votes
@Test
public void testLogLevel() throws Exception {
  setLogLevel(logger, Level.ERROR);

  Map<String, Long> expectedCounts = getCurrentCounts();
  expectedCounts.compute("total", (s, aLong) -> aLong + 2);
  expectedCounts.compute("error", (s, aLong) -> aLong + 1);
  expectedCounts.compute("fatal", (s, aLong) -> aLong + 1);

  logger.trace("trace!");
  logger.debug("debug!");
  logger.info("info!");
  logger.warn("warn!");
  logger.error("error!");
  logger.fatal("fatal!");

  assertEquals(expectedCounts, getCurrentCounts());
}
 
Example 5
Project: hack   File: KDiffPairsInAnArray.java   View Source Code Vote up 6 votes
public int findPairs(int[] nums, int k) {
    Map<Integer, Integer> map = new HashMap<>();

    for (int num : nums) {
        map.compute(num, (key, value) -> value == null ? 1 : value + 1);
    }

    int result = 0;

    for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
        if ((k > 0 && map.get(entry.getKey() + k) != null) || (k == 0 && entry.getValue() >= 2)) {
            result++;
        }
    }

    return result;
}
 
Example 6
Project: JInsight   File: Log4JInstrumentationTest.java   View Source Code Vote up 6 votes
@Test
public void testLogLevel() throws Exception {
  logger.setLevel(Level.ERROR);

  Map<String, Long> expectedCounts = getCurrentCounts();
  expectedCounts.compute("total", (s, aLong) -> aLong + 2);
  expectedCounts.compute("error", (s, aLong) -> aLong + 1);
  expectedCounts.compute("fatal", (s, aLong) -> aLong + 1);

  logger.trace("trace!");
  logger.debug("debug!");
  logger.info("info!");
  logger.warn("warn!");
  logger.error("error!");
  logger.fatal("fatal!");

  assertEquals(expectedCounts, getCurrentCounts());
}
 
Example 7
Project: JInsight   File: Log4JInstrumentationTest.java   View Source Code Vote up 6 votes
@Test
public void testLoggerReconfiguration() throws Exception {
  logger.setLevel(Level.ERROR);

  Map<String, Long> expectedCounts = getCurrentCounts();
  expectedCounts.compute("total", (s, aLong) -> aLong + 1);
  expectedCounts.compute("error", (s, aLong) -> aLong + 1);

  Properties properties = new Properties();
  properties.setProperty("log4j.rootCategory", "INFO,TestLog");
  properties.setProperty("log4j.appender.TestLog", "org.apache.log4j.ConsoleAppender");
  properties.setProperty("log4j.appender.TestLog.layout", "org.apache.log4j.PatternLayout");
  PropertyConfigurator.configure(properties);

  logger.error("error!");
  assertEquals(expectedCounts, getCurrentCounts());

}
 
Example 8
Project: Shuriken   File: Utils.java   View Source Code Vote up 5 votes
public static Path generateTestJar(Pair<String, byte[]>... files) {
    Path jarFile;
    try {
        jarFile = Files.createTempFile("testjar", ".jar");
        try(ZipOutputStream zos = new ZipOutputStream(Files.newOutputStream(jarFile))) {
            Map<String, List<Pair<String, byte[]>>> filesMap = new HashMap<>();
            for (Pair<String, byte[]> file : files) {
                filesMap.compute(file.getKey(), (k, data) -> {
                    List<Pair<String, byte[]>> fileData = data != null ? data : new ArrayList<>();
                    fileData.add(new Pair<>(file.getKey(), compress(file.getValue())));
                    return fileData;
                });
            }

            filesMap.forEach((path, data) -> {
                putPath(Paths.get(path).getParent().toString(), zos);
                data.forEach(file -> putFile(file.getKey(), file.getValue(), zos));
            });

            zos.closeEntry();
            zos.finish();
        }
    } catch (Throwable e) {
        SneakyThrow.throwException(e);
        return null;
    }
    //jarFile.toFile().deleteOnExit();
    return jarFile;
}
 
Example 9
Project: JInsight   File: LogbackInstrumentationTest.java   View Source Code Vote up 5 votes
@Test
public void testLogTrace() throws Exception {
  Map<String, Long> expectedCounts = getCurrentCounts();
  expectedCounts.compute("total", (s, aLong) -> aLong + 1);
  expectedCounts.compute("trace", (s, aLong) -> aLong + 1);

  logger.trace("TRACE!");

  assertEquals(expectedCounts, getCurrentCounts());
}
 
Example 10
Project: JInsight   File: Log4J2InstrumentationTest.java   View Source Code Vote up 5 votes
@Test
public void testLogWarn() throws Exception {
  Map<String, Long> expectedCounts = getCurrentCounts();
  expectedCounts.compute("total", (s, aLong) -> aLong + 1);
  expectedCounts.compute("warn", (s, aLong) -> aLong + 1);

  logger.warn("WARN!");

  assertEquals(expectedCounts, getCurrentCounts());
}
 
Example 11
Project: JInsight   File: Log4J2InstrumentationTest.java   View Source Code Vote up 5 votes
@Test
public void testLogInfo() throws Exception {
  Map<String, Long> expectedCounts = getCurrentCounts();
  expectedCounts.compute("total", (s, aLong) -> aLong + 1);
  expectedCounts.compute("info", (s, aLong) -> aLong + 1);

  logger.info("INFO!");

  assertEquals(expectedCounts, getCurrentCounts());
}
 
Example 12
Project: JInsight   File: LogbackInstrumentationTest.java   View Source Code Vote up 5 votes
@Test
public void testLogWarn() throws Exception {
  Map<String, Long> expectedCounts = getCurrentCounts();
  expectedCounts.compute("total", (s, aLong) -> aLong + 1);
  expectedCounts.compute("warn", (s, aLong) -> aLong + 1);

  logger.warn("WARN!");

  assertEquals(expectedCounts, getCurrentCounts());
}
 
Example 13
Project: JInsight   File: LogbackInstrumentationTest.java   View Source Code Vote up 5 votes
@Test
public void testLogError() throws Exception {
  Map<String, Long> expectedCounts = getCurrentCounts();
  expectedCounts.compute("total", (s, aLong) -> aLong + 1);
  expectedCounts.compute("error", (s, aLong) -> aLong + 1);

  logger.error("ERROR!");

  assertEquals(expectedCounts, getCurrentCounts());
}
 
Example 14
Project: JInsight   File: Log4J2InstrumentationTest.java   View Source Code Vote up 5 votes
@Test
public void testLoggerReconfiguration() throws Exception {
  setLogLevel(logger, Level.ERROR);

  Map<String, Long> expectedCounts = getCurrentCounts();
  expectedCounts.compute("total", (s, aLong) -> aLong + 1);
  expectedCounts.compute("error", (s, aLong) -> aLong + 1);

  Properties properties = new Properties();
  properties.setProperty("name", "PropertiesConfig");
  properties.setProperty("appenders", "console");
  properties.setProperty("appender.console.type", "Console");
  properties.setProperty("appender.console.name", "STDOUT");
  properties.setProperty("rootLogger.level", "debug");
  properties.setProperty("rootLogger.appenderRefs", "stdout");
  properties.setProperty("rootLogger.appenderRefs", "stdout");
  properties.setProperty("rootLogger.appenderRef.stdout.ref", "STDOUT");
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  properties.store(baos, null);
  ConfigurationSource source = new ConfigurationSource(
      new ByteArrayInputStream(baos.toByteArray()));

  PropertiesConfigurationFactory factory = new PropertiesConfigurationFactory();
  LoggerContext context = (LoggerContext) LogManager.getContext(false);
  PropertiesConfiguration configuration = factory.getConfiguration(context, source);
  configuration.start();
  context.updateLoggers(configuration);

  logger.error("error!");
  assertEquals(expectedCounts, getCurrentCounts());

}
 
Example 15
Project: openjdk-jdk10   File: PrevisitorTest.java   View Source Code Vote up 5 votes
@Override
public ResourcePool transform(ResourcePool inResources, ResourcePoolBuilder outResources) {
    if (!isPrevisitCalled) {
        throw new AssertionError("Previsit was not called");
    }
    CustomStringTable table = (CustomStringTable)((ResourcePoolImpl)inResources).getStringTable();
    if (table.size() == 0) {
        throw new AssertionError("Table is empty");
    }
    Map<String, Integer> count = new HashMap<>();
    for (int i = 0; i < table.size(); ++i) {
        String s = table.getString(i);
        Optional<ResourcePoolEntry> e = inResources.findEntry(s);
        if (e.isPresent()) {
            throw new AssertionError();
        }
        count.compute(s, (k, c) -> 1 + (c == null ? 0 : c));
    }
    count.forEach((k, v) -> {
        if (v != 1) {
            throw new AssertionError("Expected one entry in the table, got: " + v + " for " + k);
        }
    });
    inResources.entries().forEach(r -> {
        outResources.add(r);
    });

    return outResources.build();
}
 
Example 16
Project: JInsight   File: Log4JInstrumentationTest.java   View Source Code Vote up 5 votes
@Test
public void testLogInfo() throws Exception {
  Map<String, Long> expectedCounts = getCurrentCounts();
  expectedCounts.compute("total", (s, aLong) -> aLong + 1);
  expectedCounts.compute("info", (s, aLong) -> aLong + 1);

  logger.info("INFO!");

  assertEquals(expectedCounts, getCurrentCounts());
}
 
Example 17
Project: aws-sdk-java-v2   File: ProfileFile.java   View Source Code Vote up 5 votes
private void addToAggregate(Map<String, Map<String, String>> aggregateRawProfiles, ProfileFile file) {
    Map<String, Profile> profiles = file.profiles();
    for (Map.Entry<String, Profile> profile : profiles.entrySet()) {
        aggregateRawProfiles.compute(profile.getKey(), (k, current) -> {
            if (current == null) {
                return new HashMap<>(profile.getValue().properties());
            } else {
                current.putAll(profile.getValue().properties());
                return current;
            }
        });
    }
}
 
Example 18
Project: JInsight   File: Log4J2InstrumentationTest.java   View Source Code Vote up 5 votes
@Test
public void testThrowable() throws Exception {
  Map<String, Long> expectedCounts = getCurrentCounts();
  expectedCounts.compute("total", (s, aLong) -> aLong + 1);
  expectedCounts.compute("error", (s, aLong) -> aLong + 1);
  expectedCounts.compute("throwCount", (s, aLong) -> aLong + 1);
  expectedCounts.compute("throw[RuntimeException]", (s, aLong) -> aLong + 1);

  RuntimeException exception = new RuntimeException();
  exception.setStackTrace(new StackTraceElement[0]);
  logger.error("Error with throwable", exception);

  assertEquals(expectedCounts, getCurrentCounts());
}
 
Example 19
Project: JInsight   File: Log4J2InstrumentationTest.java   View Source Code Vote up 5 votes
@Test
public void testLogFatal() throws Exception {
  Map<String, Long> expectedCounts = getCurrentCounts();
  expectedCounts.compute("total", (s, aLong) -> aLong + 1);
  expectedCounts.compute("fatal", (s, aLong) -> aLong + 1);

  logger.fatal("FATAL!");

  assertEquals(expectedCounts, getCurrentCounts());
}
 
Example 20
Project: OperatieBRP   File: ElementWaarde.java   View Source Code Vote up 5 votes
private static void addSoortBericht(final Map<Integer, EnumSet<SoortBericht>> berichtElementMap, final String regel) {
    final String[] split = StringUtils.split(regel, ",");
    final Integer elementId = Integer.valueOf(split[0]);
    final SoortBericht soortBericht = SoortBericht.parseId(Integer.valueOf(split[1]));
    berichtElementMap.compute(elementId, (integer, soortBerichts) -> {
        if (soortBerichts == null) {
            return EnumSet.of(soortBericht);
        } else {
            soortBerichts.add(soortBericht);
            return soortBerichts;
        }
    });
}