下面列出了怎么用org.apache.logging.log4j.util.TriConsumer的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
@SuppressWarnings("unchecked")
public <VAL, STATE> void forEach(final TriConsumer<String, ? super VAL, STATE> action, final STATE state) {
final int startSize = size;
final K myKeys[] = this.keys;
int pos = arraySize;
iterating = true;
try {
if (containsNullKey) {
action.accept((String) myKeys[pos], (VAL) values[pos], state);
if (size != startSize) {
throw new ConcurrentModificationException();
}
}
--pos;
for (; pos >= 0; pos--) {
if (myKeys[pos] != null) {
action.accept((String) myKeys[pos], (VAL) values[pos], state);
if (size != startSize) {
throw new ConcurrentModificationException();
}
}
}
} finally {
iterating = false;
}
}
@Override
public <V, S> void forEach(final TriConsumer<String, ? super V, S> action, final S state) {
final Map<String, String> map = localMap.get();
if (map == null) {
return;
}
for (final Map.Entry<String, String> entry : map.entrySet()) {
//TriConsumer should be able to handle values of any type V. In our case the values are of type String.
@SuppressWarnings("unchecked")
V value = (V) entry.getValue();
action.accept(entry.getKey(), value, state);
}
}
@SuppressWarnings("unchecked")
@Override
public <V, S> void forEach(final TriConsumer<String, ? super V, S> action, final S state) {
final String[] keys = getSortedKeys();
for (int i = 0; i < keys.length; i++) {
action.accept(keys[i], (V) map.get(keys[i]), state);
}
}
@Test
public void testNoConcurrentModificationTriConsumerPut() {
final JdkMapAdapterStringMap original = new JdkMapAdapterStringMap();
original.putValue("a", "aaa");
original.putValue("b", "aaa");
original.putValue("d", "aaa");
original.putValue("e", "aaa");
original.forEach(new TriConsumer<String, Object, Object>() {
@Override
public void accept(final String s, final Object o, final Object o2) {
original.putValue("c", "other");
}
}, null);
}
@Test
public void testNoConcurrentModificationTriConsumerPutValue() {
final JdkMapAdapterStringMap original = new JdkMapAdapterStringMap();
original.putValue("a", "aaa");
original.putValue("b", "aaa");
original.putValue("c", "aaa");
original.putValue("d", "aaa");
original.putValue("e", "aaa");
original.forEach(new TriConsumer<String, Object, Object>() {
@Override
public void accept(final String s, final Object o, final Object o2) {
original.putValue("c" + s, "other");
}
}, null);
}
@Test
public void testNoConcurrentModificationTriConsumerRemove() {
final JdkMapAdapterStringMap original = new JdkMapAdapterStringMap();
original.putValue("a", "aaa");
original.putValue("b", "aaa");
original.putValue("c", "aaa");
original.forEach(new TriConsumer<String, Object, Object>() {
@Override
public void accept(final String s, final Object o, final Object o2) {
original.remove("a");
}
}, null);
}
@Test
public void testNoConcurrentModificationTriConsumerClear() {
final JdkMapAdapterStringMap original = new JdkMapAdapterStringMap();
original.putValue("a", "aaa");
original.putValue("b", "aaa");
original.putValue("c", "aaa");
original.putValue("d", "aaa");
original.forEach(new TriConsumer<String, Object, Object>() {
@Override
public void accept(final String s, final Object o, final Object o2) {
original.clear();
}
}, null);
}
public void addRuleObserver(TriConsumer<ServerCommandSource, ParsedRule<?>, String> observer)
{
observers.add(observer);
}
public static HashMap<Block, TriConsumer<World, BlockPos, BlockState>> getRegister() {
return register;
}
private TriConsumer<GameMap, List<Player>, Boolean> getGameCompleteHandler() {
return (gameMap, players, matchSuccessful) -> {
GamePlayer winningPlayer = gameMap.getWinningPlayer();
Player winner = players.stream()
.filter(p -> p.getGamePlayer() == winningPlayer)
.findFirst().orElse(null);
StringBuilder winnerStringBuilder = new StringBuilder();
for (Player player : players) {
winnerStringBuilder.append(player.getName()
+ "- score:" + player.getGamePlayer().getScore()
+ " health:" + player.getGamePlayer().getHealth()
+ "\n");
}
log.info("=======================================");
log.info((winner == null)
? "The game ended in a tie"
: "The winner is: " + winner.getName());
log.info("=======================================");
try {
String roundLocation = String.format("%s/%s/endGameState.txt", gameName, FileUtils.getRoundDirectory(gameMap.getCurrentRound()));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(new File(roundLocation)));
if (winner == null) {
winnerStringBuilder.insert(0, "The game ended in a tie" + "\n\n");
} else {
winnerStringBuilder.insert(0, "The winner is: " + winner.getName() + "\n\n");
}
if (!matchSuccessful) {
winnerStringBuilder.insert(0, "Bot did nothing too many consecutive rounds" + "\n\n");
}
bufferedWriter.write(winnerStringBuilder.toString());
bufferedWriter.flush();
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
};
}
@Override
@SuppressWarnings("unchecked")
public <V, S> void forEach(TriConsumer<String, ? super V, S> action, S state) {
map.forEach((key, value) -> action.accept(key, (V) value, state));
}
@Override
public <V, S> void forEach(TriConsumer<String, ? super V, S> action, S state) {
super.forEach((k,v) -> action.accept(k, (V) v, state));
}
@Override
public <V, S> void forEach(final TriConsumer<String, ? super V, S> action, final S state) {
// do nothing
}
/**
* Performs the given action for each key-value pair in this data structure
* until all entries have been processed or the action throws an exception.
* <p>
* The third parameter lets callers pass in a stateful object to be modified with the key-value pairs,
* so the TriConsumer implementation itself can be stateless and potentially reusable.
* </p>
* <p>
* Some implementations may not support structural modifications (adding new elements or removing elements) while
* iterating over the contents. In such implementations, attempts to add or remove elements from the
* {@code TriConsumer}'s {@link TriConsumer#accept(Object, Object, Object) accept} method may cause a
* {@code ConcurrentModificationException} to be thrown.
* </p>
*
* @param action The action to be performed for each key-value pair in this collection
* @param state the object to be passed as the third parameter to each invocation on the specified
* triconsumer
* @param <CV> type of the consumer value
* @param <S> type of the third parameter
* @throws java.util.ConcurrentModificationException some implementations may not support structural modifications
* to this data structure while iterating over the contents with {@link #forEach(BiConsumer)} or
* {@link #forEach(TriConsumer, Object)}.
* @see ReadOnlyStringMap#forEach(TriConsumer, Object)
* @since 2.9
*/
public <CV, S> void forEach(final TriConsumer<String, ? super CV, S> action, final S state) {
data.forEach(action, state);
}
@Override
public <V, S> void forEach(final TriConsumer<String, ? super V, S> action, final S state) {
}