Created
November 22, 2018 15:57
-
-
Save Glorp/4a4bd1e231c53581022e75e510f6ad9f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// to run: jshell caesar.jsh | |
import java.util.concurrent.ScheduledExecutorService; | |
import java.util.concurrent.ScheduledThreadPoolExecutor; | |
import java.util.concurrent.TimeUnit; | |
import java.io.PrintStream; | |
public class Caechar { | |
public static Caechar fromAlphabet(String str) { | |
Caechar first = null; | |
Caechar current = null; | |
for (int i = 0; i < str.length(); i++) { | |
Caechar next = new Caechar(str.charAt(i)); | |
if (first == null) first = next; | |
if (current != null) { | |
next.previous = current; | |
current.next = next; | |
} | |
current = next; | |
} | |
current.next = first; | |
first.previous = current; | |
return first; | |
} | |
private final char c; | |
private Caechar(char c) { | |
this.c = c; | |
} | |
private Caechar next; | |
public char getC() { | |
return c; | |
} | |
public Caechar getNext() { | |
return next; | |
} | |
public Caechar getPrevious() { | |
return previous; | |
} | |
private Caechar previous; | |
public Caechar find(char c) { | |
Caechar first = this; | |
if (first.c == c) return first; | |
for (Caechar current = first.next; current != first; current = current.next) { | |
if (current.c == c) return current; | |
} | |
return null; | |
} | |
} | |
public class Caecounter { | |
public enum Direction { | |
Forward, | |
Backward, | |
} | |
public Caechar getC() { | |
return c; | |
} | |
private void step(Direction dir) { | |
if (dir == Direction.Forward) this.c = c.getNext(); | |
if (dir == Direction.Backward) this.c = c.getPrevious(); | |
} | |
private Caechar c; | |
public Caecounter(Caechar c, Direction dir) { | |
this.c = c; | |
ScheduledExecutorService service = new ScheduledThreadPoolExecutor(1); | |
service.scheduleAtFixedRate(() -> step(dir), 1, 1, TimeUnit.SECONDS); | |
} | |
} | |
public class Caeservice { | |
private final Caechar alphabet; | |
private final PrintStream out; | |
public Caeservice(Caechar alphabet, PrintStream out) { | |
this.alphabet = alphabet; | |
this.out = out; | |
} | |
private void crypt(String s, Caecounter.Direction dir, int shift) { | |
ScheduledExecutorService service = new ScheduledThreadPoolExecutor(s.length()); | |
for (int i = 0; i < s.length(); i++) { | |
final char ch = s.charAt(i); | |
Caechar c = alphabet.find(ch); | |
if (c == null) { | |
service.schedule(() -> out.print(ch), (shift * 1000) + 500, TimeUnit.MILLISECONDS); | |
} else { | |
Caecounter cn = new Caecounter(c, dir); | |
service.schedule(() -> out.print(cn.getC().getC()), (shift * 1000) + 500, TimeUnit.MILLISECONDS); | |
} | |
try { | |
TimeUnit.SECONDS.sleep(1); | |
} catch (InterruptedException e) { | |
} | |
} | |
} | |
public void decrypt(String s, int shift) { | |
crypt(s, Caecounter.Direction.Backward, shift); | |
} | |
public void encrypt(String s, int shift) { | |
crypt(s, Caecounter.Direction.Forward, shift); | |
} | |
} | |
String s = "lawlyplujlgpzg\u00E6olg\u00E6lhjolygvmghssg\u00E6opunz\n" + | |
"uvgvulgpzgzvgiyh\u00E5lg\u00E6oh\u00E6golgpzguv\u00E6gkpz\u00E6\u00F8yilkgibgzvtl\u00E6opung\u00F8ulawlj\u00E6lk\n" + | |
"pgohkgyh\u00E6olygilgmpyz\u00E6gpughg\u00E5psshnlg\u00E6ohugzljvukgh\u00E6gyvtl\n" + | |
"tlugmyllsbgilspl\u00E5lg\u00E6oh\u00E6g opjog\u00E6olbgklzpyl\n" + | |
"pgjhtlgpgzh gpgjvux\u00F8lylk"; | |
Caeservice service = new Caeservice(Caechar.fromAlphabet("abcdefghijklmnopqrstuvwxyz\u00E6\u00F8\u00E5 "), System.out); | |
service.decrypt(s, 7); | |
TimeUnit.MILLISECONDS.sleep(7500); | |
/exit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment