/** * Default constructor. Instantiates new {@link SimpleStorageDescriptor}. */ public StorageDescriptor() { simpleStorageDescriptor = new SimpleStorageDescriptor(); }
/** * Joins the position and size information if possible contained in other descriptor. This * method will return true if the join was successfully done, and false if no join was done. The * join is possible only if the given position and size is pointing to the data that is next to * the data currently described in {@link SimpleStorageDescriptor}. * * @param other * Descriptor to join * @return This method will return true if the join was successfully done, and false if no join * was done. */ public boolean join(SimpleStorageDescriptor other) { return join(other.getPosition(), other.getSize()); }
/** * {@inheritDoc} */ @Override public int hashCode() { final int prime = 31; int result = 1; result = (prime * result) + channelId; result = (prime * result) + ((simpleStorageDescriptor == null) ? 0 : simpleStorageDescriptor.hashCode()); return result; }
/** * {@inheritDoc} */ @Override public void setPositionAndSize(long position, long size) { simpleStorageDescriptor.setPosition(position); simpleStorageDescriptor.setSize((int) size); }
/** * Adds the written position and size by updating the existing {@link #descriptors} list. * * @param position * Position that was written. * @param size * Size. */ private synchronized void addPositionAndSize(long position, long size) { for (SimpleStorageDescriptor storageDescriptor : descriptors) { if (((storageDescriptor.getSize() + size) < MAX_RANGE_SIZE) && storageDescriptor.join(position, size)) { return; } } SimpleStorageDescriptor storageDescriptor = new SimpleStorageDescriptor(position, (int) size); descriptors.add(storageDescriptor); }
/** * {@inheritDoc} */ @Override public String toString() { ToStringBuilder toStringBuilder = new ToStringBuilder(this); toStringBuilder.append("channelId", channelId); toStringBuilder.append("position", simpleStorageDescriptor.getPosition()); toStringBuilder.append("size", simpleStorageDescriptor.getSize()); return toStringBuilder.toString(); }
/** * Optimizes the list of the descriptors so that necessary joining is done. This method will * also assure that no descriptor has bigger size than {@value #MAX_RANGE_SIZE} bytes, as * defined in {@link #MAX_RANGE_SIZE}. * * @param descriptorList * List of {@link StorageDescriptor}s to optimize. */ private void optimiseDescriptors(List<SimpleStorageDescriptor> descriptorList) { for (int i = 0; i < (descriptorList.size() - 1); i++) { for (int j = i + 1; j < descriptorList.size(); j++) { SimpleStorageDescriptor descriptor = descriptors.get(i); SimpleStorageDescriptor other = descriptors.get(j); if (((descriptor.getSize() + other.getSize()) < MAX_RANGE_SIZE) && descriptor.join(other)) { descriptorList.remove(j); j--; } } } }
/** * {@inheritDoc} */ @Override public long getPosition() { return simpleStorageDescriptor.getPosition(); }
/** * {@inheritDoc} */ @Override public long getSize() { return simpleStorageDescriptor.getSize(); }
/** * Joins the position and size information if possible. This method will return true if the join * was successfully done, and false if no join was done. The join is possible only if the given * position and size is pointing to the data that is next to the data currently described in * {@link StorageDescriptor}. * * @param otherPosition * Position * @param otherSize * Size * @return This method will return true if the join was successfully done, and false if no join * was done. */ public boolean join(long otherPosition, long otherSize) { return simpleStorageDescriptor.join(otherPosition, otherSize); }
/** * {@inheritDoc} */ @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } StorageDescriptor other = (StorageDescriptor) obj; if (channelId != other.channelId) { return false; } if (simpleStorageDescriptor == null) { if (other.simpleStorageDescriptor != null) { return false; } } else if (!simpleStorageDescriptor.equals(other.simpleStorageDescriptor)) { return false; } return true; }
/** * Assigns the channel ID, position in file and size. * * @param channelId * Channel id to hold. * @param position * Position in file. * @param size * Size. */ public StorageDescriptor(int channelId, long position, long size) { this.channelId = channelId; this.simpleStorageDescriptor = new SimpleStorageDescriptor(position, (int) size); }