Subversion Repositories display

Rev

Rev 255 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
 * Copyright (C) 2007 Steve Ratcliffe
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *
 * Author: Steve Ratcliffe
 * Create date: Dec 28, 2007
 */

package test.display;

/**
 * Represents a section in the file.  It may have a record size and will have
 * a start and size.
 *
 * @author Steve Ratcliffe
 */

public class Section implements Comparable<Section> {
        private final String name;
        private final long start;
        private final int len;
        private int recordSize;
        private int magic;

        Section(String name, long start, int len) {
                this.name = name;
                this.len = len;
                this.start = start;
        }

        public int compareTo(Section o) {
                if (start > o.start)
                        return 1;
                else if (start < o.start)
                        return -1;
                else {
                        if (len > o.len)
                                return 1;
                        else if (len < o.len)
                                return -1;
                        else
                                return 0;
                }
        }

        public long getEnd() {
                return start + len;
        }

        public long getStart() {
                return start;
        }

        public void setRecordSize(int reclen) {
                this.recordSize = reclen;
        }

        public int getRecordSize() {
                return recordSize;
        }

        /**
         * Get the number of bytes required to represent any record number in this section.
         * Only valid if there is a record size set.
         * @return The minimum number of bytes required to represent the max record number.
         */

        public int getBytesForRecords() {
                return numberOfBytes((long) getNumberOfRecords());
        }

        public int getBytesForSize() {
                return numberOfBytes((long) getLen());
        }

        public static int numberOfBytes(long n) {
                if (n <= 0xff) {
                        return 1;
                } else if (n < 0xffff) {
                        return 2;
                } else if (n < 0xffffff) {
                        return 3;
                } else {
                        return 4;
                }
        }

        public int getLen() {
                return len;
        }

        public String getName() {
                return name;
        }

        /**
         * Number of items. Only valid when there is a recsize set.
         */

        public int getNumberOfRecords() {
                return (recordSize == 0) ? 0 : len/ recordSize;
        }

        public int getMagic() {
                return magic;
        }

        public void setMagic(int magic) {
                this.magic = magic;
        }
}