Debian Codegraph @

src/uk/me/parabola/imgfmt/app/srt/SrtSortKey.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2010.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 3 or
6 * version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 */
13
14 package uk.me.parabola.imgfmt.app.srt;
15
16 import java.util.Arrays;
17
18 /**
19 * Sort key created from a Srt {@link Sort} object that allows strings to be compared according to that sorting
20 * scheme.
21 *
22 * @author Steve Ratcliffe
23 */
24 class SrtSortKey<T> implements SortKey<T> {
25 private final T orig;
26 private final byte[] key;
27 private int second;
28
29 public SrtSortKey(T orig, byte[] key) {
30 this.orig = orig;
31 this.key = key;
32 }
33
34 public SrtSortKey(T orig, byte[] key, int second) {
35 this.orig = orig;
36 this.key = key;
37 this.second = second;
38 }
39
40 public int compareTo(SortKey<T> o) {
41 SrtSortKey<T> other = (SrtSortKey<T>) o;
42 if (key != other.key) {
43 int length = Math.min(this.key.length, other.key.length);
44 for (int i = 0; i < length; i++) {
45 int k1 = this.key[i] & 0xff;
46 int k2 = other.key[i] & 0xff;
47 if (k1 < k2) {
48 return -1;
49 } else if (k1 > k2) {
50 return 1;
51 }
52 }
53 }
54 //if (this.key.length < other.key.length)
55 // return -1;
56 //else if (this.key.length > other.key.length)
57 // return 1;
58
59 if (second == other.second)
60 return 0;
61 else if (second < other.second)
62 return -1;
63 else
64 return 1;
65 }
66
67 public T getObject() {
68 return orig;
69 }
70
71 public String toString() {
72 return String.format("%s,%d", Arrays.toString(key), second);
73 }
74 }