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