init
This commit is contained in:
109
Enigma/Rotor.java
Normal file
109
Enigma/Rotor.java
Normal file
@@ -0,0 +1,109 @@
|
||||
/**
|
||||
* Class for the rotors of the Enigma.
|
||||
*
|
||||
*/
|
||||
public class Rotor implements EnigmaComponent {
|
||||
/**
|
||||
* Position where rotor "ticks" next rotor.
|
||||
*/
|
||||
private int tickPos;
|
||||
|
||||
/**
|
||||
* Current position of the rotor.
|
||||
*/
|
||||
private int initialPos;
|
||||
|
||||
/**
|
||||
* If Rotor has succesor.
|
||||
*/
|
||||
private Rotor next;
|
||||
|
||||
/**
|
||||
* Permutation of the rotor.
|
||||
*/
|
||||
private int[] permu;
|
||||
|
||||
/**
|
||||
* Inverse permutation of the rotor.
|
||||
*/
|
||||
private int[] invPermu;
|
||||
|
||||
/**
|
||||
* @param tickPos Position where rotor "ticks" next rotor
|
||||
* @param initialPos Current position of the rotor
|
||||
* @param permu Permutation of the rotor
|
||||
*/
|
||||
Rotor(final int tickPos, final int initialPos, final int[] permu) {
|
||||
this.tickPos = tickPos;
|
||||
this.initialPos = initialPos;
|
||||
this.permu = permu;
|
||||
this.invPermu = calcInvPermu(permu);
|
||||
this.next = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter for the next-rotor-pointer.
|
||||
* @param next Next rotor
|
||||
*/
|
||||
public final void setNext(final Rotor next) {
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the inverese permutation.
|
||||
* @param permu permutation
|
||||
* @return <code>inverse permutation</code>
|
||||
*/
|
||||
private int[] calcInvPermu(final int[] permu) {
|
||||
int[] array = new int[permu.length];
|
||||
for (int i = 0; i < permu.length; i++) {
|
||||
for (int k = 0; k < permu.length; k++) {
|
||||
if (i == permu[k]) {
|
||||
array[i] = k;
|
||||
}
|
||||
}
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public final int encode(final int i) {
|
||||
if (((permu[((initialPos + i) % permu.length)] - initialPos
|
||||
+ permu.length) % permu.length) < 0) {
|
||||
return (((permu[((initialPos + i) % permu.length)]
|
||||
- initialPos + permu.length)
|
||||
% permu.length) + permu.length);
|
||||
}
|
||||
return (permu[((initialPos + i) % permu.length)]
|
||||
- initialPos + permu.length) % permu.length;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public final int decode(final int i) {
|
||||
if (((invPermu[((initialPos + i) % permu.length)]
|
||||
- initialPos + permu.length) % permu.length) < 0) {
|
||||
return (((invPermu[((initialPos + i) % permu.length)]
|
||||
- initialPos + permu.length)
|
||||
% permu.length) + permu.length);
|
||||
}
|
||||
return (invPermu[((initialPos + i) % permu.length)]
|
||||
- initialPos + permu.length) % permu.length;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public final void tick() {
|
||||
if (initialPos == permu.length) {
|
||||
initialPos = 0;
|
||||
}
|
||||
initialPos++;
|
||||
if (initialPos == (tickPos + 1) && next != null) {
|
||||
next.tick();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user