home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2005 June / PCpro_2005_06.ISO / files / opensource / tvbrowser / TvBrowser_1.0.exe / plugins / ShowviewPlugin.jar / showviewplugin / ShowviewEncoder.class (.txt) < prev    next >
Encoding:
Java Class File  |  2004-09-19  |  5.2 KB  |  200 lines

  1. package showviewplugin;
  2.  
  3. import devplugin.Channel;
  4. import devplugin.Date;
  5. import devplugin.Program;
  6. import util.exc.TvBrowserException;
  7.  
  8. public class ShowviewEncoder {
  9.    private static ShowviewEncoder mSingleton;
  10.    // $FF: synthetic field
  11.    static Class class$showviewplugin$ShowviewEncoder;
  12.  
  13.    private ShowviewEncoder() {
  14.    }
  15.  
  16.    public static ShowviewEncoder getInstance() {
  17.       if (mSingleton == null) {
  18.          mSingleton = new ShowviewEncoder();
  19.       }
  20.  
  21.       return mSingleton;
  22.    }
  23.  
  24.    public String getShowviewNumberFor(Program prog) throws TvBrowserException {
  25.       Channel channel = prog.getChannel();
  26.       Date date = prog.getDate();
  27.       int year = date.getYear();
  28.       int month = date.getMonth();
  29.       int day = date.getDayOfMonth();
  30.       int hour = prog.getHours();
  31.       int minute = prog.getMinutes();
  32.       int length = prog.getLength();
  33.       return this.getShowviewNumberFor(channel, year, month, day, hour, minute, length);
  34.    }
  35.  
  36.    public String getShowviewNumberFor(Channel channel, int year, int month, int day, int hour, int minute, int length) throws TvBrowserException {
  37.       year %= 100;
  38.       int minDiff = minute % 5;
  39.       minute -= minDiff;
  40.       length += minDiff;
  41.       int channelNumber = ShowviewChannelTable.getChannelNumberFor(channel);
  42.       if (channelNumber == -1) {
  43.          throw new TvBrowserException(class$showviewplugin$ShowviewEncoder == null ? (class$showviewplugin$ShowviewEncoder = class$("showviewplugin.ShowviewEncoder")) : class$showviewplugin$ShowviewEncoder, "error.1", "Didn't find a showview channel number (Leitzahl) for {0}", channel);
  44.       } else {
  45.          if (channelNumber == 26) {
  46.             channelNumber += 18;
  47.          } else if (channelNumber == 109) {
  48.             channelNumber -= 59;
  49.          } else {
  50.             if (channelNumber >= 19) {
  51.                throw new TvBrowserException(class$showviewplugin$ShowviewEncoder == null ? (class$showviewplugin$ShowviewEncoder = class$("showviewplugin.ShowviewEncoder")) : class$showviewplugin$ShowviewEncoder, "error.2", "Calculation does not work for this program");
  52.             }
  53.  
  54.             --channelNumber;
  55.          }
  56.  
  57.          int timeIndex = ShowviewTimeTable.getInstance().getBestFit(hour, minute, length);
  58.          int top2 = ((timeIndex >> 2 & 1) << 4) + ((channelNumber >> 1 & 1) << 3) + ((timeIndex >> 1 & 1) << 2) + ((channelNumber & 1) << 1) + (timeIndex & 1);
  59.          int newtop = ((timeIndex >> 9 & 1) << 9) + ((channelNumber >> 4 & 1) << 8) + ((channelNumber >> 3 & 1) << 7) + ((timeIndex >> 8 & 1) << 6) + ((timeIndex >> 7 & 1) << 5) + ((timeIndex >> 6 & 1) << 4) + ((timeIndex >> 5 & 1) << 3) + ((timeIndex >> 4 & 1) << 2) + ((timeIndex >> 3 & 1) << 1) + (channelNumber >> 2 & 1);
  60.          if (newtop > 999) {
  61.             throw new TvBrowserException(class$showviewplugin$ShowviewEncoder == null ? (class$showviewplugin$ShowviewEncoder = class$("showviewplugin.ShowviewEncoder")) : class$showviewplugin$ShowviewEncoder, "error.2", "Calculation does not work for this program");
  62.          } else {
  63.             int bottom = (day - 1) * 32 + 1;
  64.             int repeatCount = year % 16 + 1;
  65.             int[] topArr = this.splitDigits(newtop);
  66.             int newtopLength = this.getLength(newtop);
  67.             int doLoopCount = 0;
  68.             int offset = 0;
  69.  
  70.             int top;
  71.             do {
  72.                for(int i = 0; i < repeatCount; ++i) {
  73.                   for(int j = topArr.length - 1; j >= 1; --j) {
  74.                      topArr[j] = this.saveMod(topArr[j] - topArr[j - 1], 10);
  75.                   }
  76.  
  77.                   topArr[0] = this.saveMod(topArr[0] - day, 10);
  78.                }
  79.  
  80.                top = this.mergeDigits(topArr);
  81.  
  82.                for(int i = 1; i <= repeatCount; ++i) {
  83.                   int newNumber = this.calculateNewNumber(day, top, newtopLength, i);
  84.                   offset += newNumber % 10;
  85.                }
  86.  
  87.                ++doLoopCount;
  88.                if (doLoopCount > 100) {
  89.                   throw new TvBrowserException(class$showviewplugin$ShowviewEncoder == null ? (class$showviewplugin$ShowviewEncoder = class$("showviewplugin.ShowviewEncoder")) : class$showviewplugin$ShowviewEncoder, "error.2", "Calculation does not work for this program");
  90.                }
  91.             } while(this.getLength(top) < newtopLength);
  92.  
  93.             for(int i = 0; i < topArr.length; ++i) {
  94.                offset += topArr[i];
  95.             }
  96.  
  97.             int rem = this.saveMod(top2 - day * (month + 1) - offset, 32);
  98.             int code = rem + bottom + top * 1000;
  99.             repeatCount = 3;
  100.             doLoopCount = 0;
  101.             int[] showviewArr = this.splitDigits(code);
  102.  
  103.             do {
  104.                for(int i = 0; i < repeatCount; ++i) {
  105.                   for(int j = 0; j < showviewArr.length - 1; ++j) {
  106.                      showviewArr[j] = (showviewArr[j] - showviewArr[j + 1] + 10) % 10;
  107.                   }
  108.                }
  109.  
  110.                ++doLoopCount;
  111.                if (doLoopCount > 100) {
  112.                   throw new TvBrowserException(class$showviewplugin$ShowviewEncoder == null ? (class$showviewplugin$ShowviewEncoder = class$("showviewplugin.ShowviewEncoder")) : class$showviewplugin$ShowviewEncoder, "error.2", "Calculation does not work for this program");
  113.                }
  114.             } while(showviewArr[0] == 0);
  115.  
  116.             int showview = this.mergeDigits(showviewArr);
  117.             if (minDiff != 0) {
  118.                showview += minDiff * 100000000;
  119.             }
  120.  
  121.             StringBuffer buffer = new StringBuffer();
  122.             showviewArr = this.splitDigits(showview);
  123.  
  124.             for(int i = 0; i < showviewArr.length; ++i) {
  125.                buffer.append(showviewArr[i]);
  126.                int digit = showviewArr.length - i - 1;
  127.                if (digit > 0 && digit % 3 == 0) {
  128.                   buffer.append('-');
  129.                }
  130.             }
  131.  
  132.             return buffer.toString();
  133.          }
  134.       }
  135.    }
  136.  
  137.    private int calculateNewNumber(int day, int top, int newtopLength, int repeatCount) {
  138.       int[] numberArr = this.splitDigits(top, newtopLength);
  139.  
  140.       for(int i = 0; i < repeatCount; ++i) {
  141.          numberArr[0] = (numberArr[0] + day) % 10;
  142.  
  143.          for(int j = 1; j < numberArr.length; ++j) {
  144.             numberArr[j] = (numberArr[j] + numberArr[j - 1]) % 10;
  145.          }
  146.       }
  147.  
  148.       return this.mergeDigits(numberArr);
  149.    }
  150.  
  151.    private int[] splitDigits(int number) {
  152.       int length = this.getLength(number);
  153.       return this.splitDigits(number, length);
  154.    }
  155.  
  156.    private int[] splitDigits(int number, int nrDigits) {
  157.       int[] digitArr = new int[nrDigits];
  158.  
  159.       for(int i = 0; i < digitArr.length; ++i) {
  160.          int index = digitArr.length - i - 1;
  161.          digitArr[index] = number % 10;
  162.          number /= 10;
  163.       }
  164.  
  165.       return digitArr;
  166.    }
  167.  
  168.    private int mergeDigits(int[] digitArr) {
  169.       int number = 0;
  170.  
  171.       for(int i = 0; i < digitArr.length; ++i) {
  172.          number = number * 10 + digitArr[i] % 10;
  173.       }
  174.  
  175.       return number;
  176.    }
  177.  
  178.    private int getLength(int number) {
  179.       int length;
  180.       for(length = 1; number >= 10; ++length) {
  181.          number /= 10;
  182.       }
  183.  
  184.       return length;
  185.    }
  186.  
  187.    private int saveMod(int number, int base) {
  188.       return (number % base + base) % base;
  189.    }
  190.  
  191.    // $FF: synthetic method
  192.    static Class class$(String x0) {
  193.       try {
  194.          return Class.forName(x0);
  195.       } catch (ClassNotFoundException x1) {
  196.          throw new NoClassDefFoundError(x1.getMessage());
  197.       }
  198.    }
  199. }
  200.