source: trunk/src/org/ngikm/Util.java@ 1225

Last change on this file since 1225 was 1225, checked in by bln4, 5 years ago

Util.java -> Temporary (or will be moved) utility functions for Authentication code

File size: 1.6 KB
Line 
1package org.ngikm;
2
3import java.util.Optional;
4import java.util.function.Consumer;
5import java.util.function.Function;
6
7import org.expeditee.gio.EcosystemManager;
8import org.expeditee.gui.Frame;
9import org.expeditee.items.Text;
10
11public class Util {
12
13 @FunctionalInterface
14 public interface ThrowingConsumer<T, E extends Exception> extends Consumer<T> {
15 @Override
16 default void accept(final T t) {
17 try {
18 acceptThrows(t);
19 } catch (final Exception e) {
20 throw new RuntimeException(e);
21 }
22 }
23 void acceptThrows(final T t) throws E;
24 }
25
26 @FunctionalInterface
27 public interface ThrowingFunction<T, R, E extends Exception> extends Function<T, R> {
28 @Override
29 default R apply(T t) {
30 try {
31 return applyThrows(t);
32 } catch (final Exception e) {
33 throw new RuntimeException(e);
34 }
35 }
36 R applyThrows(final T t) throws E;
37 }
38
39 public static Text AddText(final Frame toFrame, final int x, final int y, final String content) {
40 final int windowHeight = EcosystemManager.getGraphicsManager().getWindowSize().height;
41 final Optional<Text> item = toFrame.getTextItems().stream().filter(i -> {
42 final org.expeditee.core.Point p = i.getPosition();
43 return (p.getX() == x) && (p.getY() == y);
44 }).findAny();
45 if (item.isPresent()) {
46 if (y >= (windowHeight * 0.9)) {
47 return AddText(toFrame, x + 100, y, content);
48 }
49 return AddText(toFrame, x, y + 100, content);
50 } else {
51 final Text text = toFrame.addText(x, y, content, null, null);
52 text.setOwner(toFrame.getOwner());
53 return text;
54 }
55 }
56}
Note: See TracBrowser for help on using the repository browser.