Mark classes as final
[controller.git] / third-party / atomix / storage / src / main / java / io / atomix / storage / journal / SegmentedJournal.java
index 07496c5dc1855e79bd7b4599db7adcb63aae2f86..ea5944aabd03f354389d42d7d758b95b0e935094 100644 (file)
@@ -30,20 +30,18 @@ import java.util.TreeMap;
 import java.util.concurrent.ConcurrentSkipListMap;
 
 import com.google.common.collect.Sets;
-import io.atomix.storage.StorageException;
-import io.atomix.storage.StorageLevel;
 import io.atomix.utils.serializer.Namespace;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import static com.google.common.base.Preconditions.checkArgument;
-import static com.google.common.base.Preconditions.checkNotNull;
 import static com.google.common.base.Preconditions.checkState;
+import static java.util.Objects.requireNonNull;
 
 /**
  * Segmented journal.
  */
-public class SegmentedJournal<E> implements Journal<E> {
+public final class SegmentedJournal<E> implements Journal<E> {
 
   /**
    * Returns a new Raft log builder.
@@ -70,7 +68,7 @@ public class SegmentedJournal<E> implements Journal<E> {
   private volatile long commitIndex;
 
   private final NavigableMap<Long, JournalSegment<E>> segments = new ConcurrentSkipListMap<>();
-  private final Collection<SegmentedJournalReader> readers = Sets.newConcurrentHashSet();
+  private final Collection<SegmentedJournalReader<E>> readers = Sets.newConcurrentHashSet();
   private JournalSegment<E> currentSegment;
 
   private volatile boolean open = true;
@@ -85,10 +83,10 @@ public class SegmentedJournal<E> implements Journal<E> {
       int maxEntriesPerSegment,
       double indexDensity,
       boolean flushOnCommit) {
-    this.name = checkNotNull(name, "name cannot be null");
-    this.storageLevel = checkNotNull(storageLevel, "storageLevel cannot be null");
-    this.directory = checkNotNull(directory, "directory cannot be null");
-    this.namespace = checkNotNull(namespace, "namespace cannot be null");
+    this.name = requireNonNull(name, "name cannot be null");
+    this.storageLevel = requireNonNull(storageLevel, "storageLevel cannot be null");
+    this.directory = requireNonNull(directory, "directory cannot be null");
+    this.namespace = requireNonNull(namespace, "namespace cannot be null");
     this.maxSegmentSize = maxSegmentSize;
     this.maxEntrySize = maxEntrySize;
     this.maxEntriesPerSegment = maxEntriesPerSegment;
@@ -359,7 +357,7 @@ public class SegmentedJournal<E> implements Journal<E> {
     assertOpen();
     assertDiskSpace();
 
-    JournalSegment lastSegment = getLastSegment();
+    JournalSegment<E> lastSegment = getLastSegment();
     JournalSegmentDescriptor descriptor = JournalSegmentDescriptor.builder()
         .withId(lastSegment != null ? lastSegment.descriptor().id() + 1 : 1)
         .withIndex(currentSegment.lastIndex() + 1)
@@ -410,7 +408,7 @@ public class SegmentedJournal<E> implements Journal<E> {
    *
    * @param segment The segment to remove.
    */
-  synchronized void removeSegment(JournalSegment segment) {
+  synchronized void removeSegment(JournalSegment<E> segment) {
     segments.remove(segment.index());
     segment.close();
     segment.delete();
@@ -552,7 +550,7 @@ public class SegmentedJournal<E> implements Journal<E> {
    * @param index The index at which to reset readers.
    */
   void resetHead(long index) {
-    for (SegmentedJournalReader reader : readers) {
+    for (SegmentedJournalReader<E> reader : readers) {
       if (reader.getNextIndex() < index) {
         reader.reset(index);
       }
@@ -565,14 +563,14 @@ public class SegmentedJournal<E> implements Journal<E> {
    * @param index The index at which to reset readers.
    */
   void resetTail(long index) {
-    for (SegmentedJournalReader reader : readers) {
+    for (SegmentedJournalReader<E> reader : readers) {
       if (reader.getNextIndex() >= index) {
         reader.reset(index);
       }
     }
   }
 
-  void closeReader(SegmentedJournalReader reader) {
+  void closeReader(SegmentedJournalReader<E> reader) {
     readers.remove(reader);
   }
 
@@ -616,7 +614,7 @@ public class SegmentedJournal<E> implements Journal<E> {
       SortedMap<Long, JournalSegment<E>> compactSegments = segments.headMap(segmentEntry.getValue().index());
       if (!compactSegments.isEmpty()) {
         log.debug("{} - Compacting {} segment(s)", name, compactSegments.size());
-        for (JournalSegment segment : compactSegments.values()) {
+        for (JournalSegment<E> segment : compactSegments.values()) {
           log.trace("Deleting segment: {}", segment);
           segment.close();
           segment.delete();
@@ -667,7 +665,7 @@ public class SegmentedJournal<E> implements Journal<E> {
   /**
    * Raft log builder.
    */
-  public static class Builder<E> implements io.atomix.utils.Builder<SegmentedJournal<E>> {
+  public static class Builder<E> {
     private static final boolean DEFAULT_FLUSH_ON_COMMIT = false;
     private static final String DEFAULT_NAME = "atomix";
     private static final String DEFAULT_DIRECTORY = System.getProperty("user.dir");
@@ -698,7 +696,7 @@ public class SegmentedJournal<E> implements Journal<E> {
      * @return The storage builder.
      */
     public Builder<E> withName(String name) {
-      this.name = checkNotNull(name, "name cannot be null");
+      this.name = requireNonNull(name, "name cannot be null");
       return this;
     }
 
@@ -711,7 +709,7 @@ public class SegmentedJournal<E> implements Journal<E> {
      * @return The storage builder.
      */
     public Builder<E> withStorageLevel(StorageLevel storageLevel) {
-      this.storageLevel = checkNotNull(storageLevel, "storageLevel cannot be null");
+      this.storageLevel = requireNonNull(storageLevel, "storageLevel cannot be null");
       return this;
     }
 
@@ -725,7 +723,7 @@ public class SegmentedJournal<E> implements Journal<E> {
      * @throws NullPointerException If the {@code directory} is {@code null}
      */
     public Builder<E> withDirectory(String directory) {
-      return withDirectory(new File(checkNotNull(directory, "directory cannot be null")));
+      return withDirectory(new File(requireNonNull(directory, "directory cannot be null")));
     }
 
     /**
@@ -738,7 +736,7 @@ public class SegmentedJournal<E> implements Journal<E> {
      * @throws NullPointerException If the {@code directory} is {@code null}
      */
     public Builder<E> withDirectory(File directory) {
-      this.directory = checkNotNull(directory, "directory cannot be null");
+      this.directory = requireNonNull(directory, "directory cannot be null");
       return this;
     }
 
@@ -749,7 +747,7 @@ public class SegmentedJournal<E> implements Journal<E> {
      * @return The journal builder.
      */
     public Builder<E> withNamespace(Namespace namespace) {
-      this.namespace = checkNotNull(namespace, "namespace cannot be null");
+      this.namespace = requireNonNull(namespace, "namespace cannot be null");
       return this;
     }
 
@@ -868,7 +866,11 @@ public class SegmentedJournal<E> implements Journal<E> {
       return this;
     }
 
-    @Override
+    /**
+     * Build the {@link SegmentedJournal}.
+     *
+     * @return A new {@link SegmentedJournal}.
+     */
     public SegmentedJournal<E> build() {
       return new SegmentedJournal<>(
           name,