001package co.codewizards.cloudstore.local.persistence;
002
003import static co.codewizards.cloudstore.core.util.Util.*;
004
005import javax.jdo.annotations.Inheritance;
006import javax.jdo.annotations.InheritanceStrategy;
007import javax.jdo.annotations.NotPersistent;
008import javax.jdo.annotations.NullValue;
009import javax.jdo.annotations.PersistenceCapable;
010import javax.jdo.annotations.Persistent;
011import javax.jdo.annotations.Unique;
012import javax.jdo.listener.LoadCallback;
013import javax.jdo.listener.StoreCallback;
014
015import co.codewizards.cloudstore.core.util.AssertUtil;
016
017@PersistenceCapable
018@Inheritance(strategy=InheritanceStrategy.NEW_TABLE)
019@Unique(name="FileChunk_normalFile_offset", members={"normalFile", "offset"})
020public class FileChunk extends Entity implements Comparable<FileChunk>, StoreCallback, LoadCallback {
021
022        @NotPersistent
023        private boolean writable = true;
024
025        @Persistent(nullValue=NullValue.EXCEPTION)
026        private NormalFile normalFile;
027
028        private long offset;
029
030        private int length;
031
032        @Persistent(nullValue=NullValue.EXCEPTION)
033        private String sha1;
034
035        public NormalFile getNormalFile() {
036                return normalFile;
037        }
038        public void setNormalFile(final NormalFile normalFile) {
039                assertWritable();
040
041                if (! equal(this.normalFile, normalFile))
042                        this.normalFile = normalFile;
043        }
044        public long getOffset() {
045                return offset;
046        }
047        public void setOffset(final long offset) {
048                assertWritable();
049
050                if (! equal(this.offset, offset))
051                        this.offset = offset;
052        }
053        public int getLength() {
054                return length;
055        }
056        public void setLength(final int length) {
057                assertWritable();
058
059                if (! equal(this.length, length))
060                        this.length = length;
061        }
062        public String getSha1() {
063                return sha1;
064        }
065        public void setSha1(final String sha1) {
066                assertWritable();
067
068                if (! equal(this.sha1, sha1))
069                        this.sha1 = sha1;
070        }
071
072        protected void assertWritable() {
073                if (!writable)
074                        throw new IllegalStateException("This instance is read-only!");
075        }
076
077        public void makeReadOnly() {
078                writable = false;
079        }
080
081        protected void makeWritable() {
082                writable = true;
083        }
084
085        @Override
086        public void jdoPreStore() {
087                makeReadOnly();
088        }
089        @Override
090        public void jdoPostLoad() {
091                makeReadOnly();
092        }
093
094        @Override
095        public int compareTo(final FileChunk other) {
096                AssertUtil.assertNotNull(other, "other");
097
098                if (this.normalFile != other.normalFile) {
099                        final long thisRepoFileId = this.normalFile == null ? 0 : this.normalFile.getId();
100                        final long otherRepoFileId = other.normalFile == null ? 0 : other.normalFile.getId();
101
102                        final int result = compare(thisRepoFileId, otherRepoFileId);
103                        if (result != 0)
104                                return result;
105                }
106
107                final int result = compare(this.offset, other.offset);
108                if (result != 0)
109                        return result;
110
111                final long thisId = this.getId();
112                final long otherId = other.getId();
113
114                return compare(thisId, otherId);
115        }
116
117        private static int compare(final long x, final long y) {
118        return (x < y) ? -1 : ((x == y) ? 0 : 1);
119    }
120
121}