java/dictionary-generator/src/cz/frantovo/telco/dictionary/IndexEntry.java
changeset 17 b188eae2c092
parent 15 93208f791318
child 130 b8dc8a6f82cf
     1.1 --- a/java/dictionary-generator/src/cz/frantovo/telco/dictionary/IndexEntry.java	Tue Jul 09 22:41:49 2013 +0200
     1.2 +++ b/java/dictionary-generator/src/cz/frantovo/telco/dictionary/IndexEntry.java	Tue Jul 09 22:42:32 2013 +0200
     1.3 @@ -17,14 +17,69 @@
     1.4   */
     1.5  package cz.frantovo.telco.dictionary;
     1.6  
     1.7 +import java.io.DataOutputStream;
     1.8 +import java.io.IOException;
     1.9 +import java.nio.charset.StandardCharsets;
    1.10 +import java.util.Objects;
    1.11 +
    1.12  /**
    1.13   *
    1.14   * @author Ing. František Kučera (frantovo.cz)
    1.15   */
    1.16  public class IndexEntry implements Comparable<IndexEntry> {
    1.17  
    1.18 +	private String name;
    1.19 +	private long offset;
    1.20 +	private long length;
    1.21 +	private long ordinal;
    1.22 +
    1.23 +	public IndexEntry(String name, long offset, long length) {
    1.24 +		this.name = name;
    1.25 +		this.offset = offset;
    1.26 +		this.length = length;
    1.27 +	}
    1.28 +
    1.29 +	public void serialize(DataOutputStream indexOutputStream) throws IOException {
    1.30 +		indexOutputStream.write(name.getBytes(StandardCharsets.UTF_8));
    1.31 +		indexOutputStream.write(0);
    1.32 +		indexOutputStream.writeInt((int) offset); // unsigned int 32
    1.33 +		indexOutputStream.writeInt((int) length); // unsigned int 32
    1.34 +	}
    1.35 +
    1.36 +	public void setOrdinal(long ordinal) {
    1.37 +		this.ordinal = ordinal;
    1.38 +	}
    1.39 +
    1.40 +	public long getOrdinal() {
    1.41 +		return ordinal;
    1.42 +	}
    1.43 +
    1.44  	@Override
    1.45  	public int compareTo(IndexEntry o) {
    1.46 -		throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    1.47 +		int nameDiff = name.compareTo(o.name);
    1.48 +		if (nameDiff == 0) {
    1.49 +			int offsetDiff = ((Long) offset).compareTo(o.offset);
    1.50 +			if (offsetDiff == 0) {
    1.51 +				return ((Long) length).compareTo(o.length);
    1.52 +			} else {
    1.53 +				return offsetDiff;
    1.54 +			}
    1.55 +		} else {
    1.56 +			return nameDiff;
    1.57 +		}
    1.58 +	}
    1.59 +
    1.60 +	@Override
    1.61 +	public boolean equals(Object o) {
    1.62 +		return o instanceof IndexEntry && compareTo((IndexEntry) o) == 0;
    1.63 +	}
    1.64 +
    1.65 +	@Override
    1.66 +	public int hashCode() {
    1.67 +		int hash = 5;
    1.68 +		hash = 53 * hash + Objects.hashCode(this.name);
    1.69 +		hash = 53 * hash + (int) (this.offset ^ (this.offset >>> 32));
    1.70 +		hash = 53 * hash + (int) (this.length ^ (this.length >>> 32));
    1.71 +		return hash;
    1.72  	}
    1.73  }