001/*
002 * Syncany, www.syncany.org
003 * Copyright (C) 2011-2016 Philipp C. Heckel <philipp.heckel@gmail.com>
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU General Public License as published by
007 * the Free Software Foundation, either version 3 of the License, or
008 * (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU General Public License for more details.
014 *
015 * You should have received a copy of the GNU General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018package org.syncany.database;
019
020/**
021 * The chunk entry represents a single chunk reference in the database model
022 * and is identified by the chunk's checksum. Due to the fact that the chunk
023 * entry is a reference, it does not contain the chunk's actual data.
024 *
025 * <p>A chunk can appear in a {@link MultiChunkEntry} and in a {@link FileContent}.
026 *
027 * @see MultiChunkEntry
028 * @see FileContent
029 * @author Philipp C. Heckel (philipp.heckel@gmail.com)
030 */
031public class ChunkEntry {
032        private ChunkChecksum checksum;
033        private int size;
034
035        public ChunkEntry(ChunkChecksum checksum, int size) {
036                this.checksum = checksum;
037                this.size = size;
038        }
039
040        public void setSize(int chunksize) {
041                size = chunksize;
042        }
043
044        public int getSize() {
045                return size;
046        }
047
048        public ChunkChecksum getChecksum() {
049                return checksum;
050        }
051
052        public void setChecksum(ChunkChecksum checksum) {
053                this.checksum = checksum;
054        }
055
056        @Override
057        public String toString() {
058                return "ChunkEntry [checksum=" + checksum + ", size=" + size + "]";
059        }
060
061        @Override
062        public int hashCode() {
063                final int prime = 31;
064                int result = 1;
065                result = prime * result + ((checksum == null) ? 0 : checksum.hashCode());
066                result = prime * result + size;
067                return result;
068        }
069
070        @Override
071        public boolean equals(Object obj) {
072                if (this == obj) {
073                        return true;
074                }
075                if (obj == null) {
076                        return false;
077                }
078                if (!(obj instanceof ChunkEntry)) {
079                        return false;
080                }
081                ChunkEntry other = (ChunkEntry) obj;
082                if (checksum == null) {
083                        if (other.checksum != null) {
084                                return false;
085                        }
086                }
087                else if (!checksum.equals(other.checksum)) {
088                        return false;
089                }
090                if (size != other.size) {
091                        return false;
092                }
093                return true;
094        }
095
096        public static class ChunkChecksum extends ObjectId {
097                public ChunkChecksum(byte[] array) {
098                        super(array);
099                }
100
101                public static ChunkChecksum parseChunkChecksum(String s) {
102                        return new ChunkChecksum(ObjectId.parseObjectId(s));
103                }
104        }
105}