Subversion Repositories distributed

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. package de.viathinksoft.distributed.apps.immortal.iterator.marschall;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.BufferedWriter;
  5. import java.io.FileReader;
  6. import java.io.FileWriter;
  7. import java.io.IOException;
  8. import java.math.BigInteger;
  9. import java.security.NoSuchAlgorithmException;
  10. import java.util.ArrayList;
  11.  
  12. import de.viathinksoft.distributed.apps.immortal.exception.FileContentsBadException;
  13. import de.viathinksoft.distributed.apps.immortal.exception.InitialNumberIsOneException;
  14. import de.viathinksoft.distributed.apps.immortal.exception.InitialNumberNotImmortableException;
  15. import de.viathinksoft.distributed.apps.immortal.iterator.QuickImmortableCheck;
  16. import de.viathinksoft.utils.security.MD5;
  17.  
  18. //TODO: Gewisse zahl anwählen
  19.  
  20. public class SectionImmortableNumber extends ArrayList<BigInteger> {
  21.  
  22.         private static final long serialVersionUID = 251721127226195138L;
  23.  
  24.         protected static final String SEPARATOR = ",";
  25.  
  26.         public SectionImmortableNumber(BigInteger initialElement)
  27.                         throws InitialNumberNotImmortableException,
  28.                         InitialNumberIsOneException {
  29.                 // TODO: initialnumberisone: soll das auch bei "0" passieren? (unendlichschleife)
  30.                 if (initialElement.equals(BigInteger.ONE)) {
  31.                         throw new InitialNumberIsOneException();
  32.                 }
  33.                 if (!QuickImmortableCheck.check(initialElement)) {
  34.                         throw new InitialNumberNotImmortableException();
  35.                 }
  36.                 this.add(initialElement);
  37.         }
  38.  
  39.         // public SectionImmortableNumber(String initialElement)
  40.         // throws InitialNumberNotImmortableException,
  41.         // InitialNumberIsOneException {
  42.         // this(new BigInteger(initialElement));
  43.         // }
  44.  
  45.         public SectionImmortableNumber(int initialElement)
  46.                         throws InitialNumberNotImmortableException,
  47.                         InitialNumberIsOneException {
  48.                 // this(Integer.toString(initialElement));
  49.                 this(new BigInteger(Integer.toString(initialElement)));
  50.         }
  51.  
  52.         public SectionImmortableNumber(String filename)
  53.                         throws NoSuchAlgorithmException, IOException,
  54.                         FileContentsBadException {
  55.                 loadFromFile(filename);
  56.         }
  57.  
  58.         public BigInteger getInitialElement() {
  59.                 return this.get(0);
  60.         }
  61.  
  62.         public int getCalculatedAmount() {
  63.                 return this.size() - 1;
  64.         }
  65.  
  66.         @Override
  67.         public String toString() {
  68.                 String s = "";
  69.                 for (BigInteger m : this) {
  70.                         if (s.equals("")) {
  71.                                 s = m.toString();
  72.                         } else {
  73.                                 s = m.toString().concat(SEPARATOR).concat(s);
  74.                         }
  75.                 }
  76.                 return s;
  77.         }
  78.  
  79.         public BigInteger toBigInteger() {
  80.                 String s = "";
  81.                 for (BigInteger m : this) {
  82.                         s = m.toString().concat(s);
  83.                 }
  84.                 return new BigInteger(s);
  85.         }
  86.  
  87.         // TODO equals, clone
  88.  
  89.         private final String SIGNATURE = "Immortal Sectioned Marschall Number V001";
  90.         private final String SALT = "AFFAüüü+++MSKGM ignsdg nkoi ajp894jfocwrvw  AFAFAü324§$§$%$&WQ";
  91.  
  92.         public void saveToFile(String filename) throws IOException,
  93.                         NoSuchAlgorithmException {
  94.                 BufferedWriter x = new BufferedWriter(new FileWriter(filename));
  95.  
  96.                 String s;
  97.                 x.write(SIGNATURE);
  98.                 x.write("\r\n");
  99.  
  100.                 s = this.toString();
  101.                 x.write(s);
  102.                 x.write("\r\n");
  103.                 x.write(MD5.digest(s, SALT));
  104.                 x.write("\r\n");
  105.  
  106.                 x.close();
  107.         }
  108.  
  109.         public void loadFromFile(String filename) throws IOException,
  110.                         NoSuchAlgorithmException, FileContentsBadException {
  111.                 BufferedReader r = new BufferedReader(new FileReader(filename));
  112.  
  113.                 String f_sig = r.readLine();
  114.                 if ((f_sig == null) || (!f_sig.equals(SIGNATURE))) {
  115.                         throw new FileContentsBadException();
  116.                 }
  117.  
  118.                 String f_commatext = r.readLine();
  119.                 if ((f_commatext == null)
  120.                                 || (!MD5.digest(f_commatext, SALT).equals(r.readLine()))) {
  121.                         throw new FileContentsBadException();
  122.                 }
  123.  
  124.                 r.close();
  125.  
  126.                 this.clear();
  127.  
  128.                         String[] ary = f_commatext.split(SEPARATOR);
  129.                         for (int i = ary.length - 1; i >= 0; i--) {
  130.                                 this.add(new BigInteger(ary[i]));
  131.                         }
  132.                         // for (String s : ary) {
  133.                         // this.add(new BigInteger(s));
  134.                         // }
  135.         }
  136.  
  137. }
  138.