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