Change.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one
  3.  * or more contributor license agreements.  See the NOTICE file
  4.  * distributed with this work for additional information
  5.  * regarding copyright ownership.  The ASF licenses this file
  6.  * to you under the Apache License, Version 2.0 (the
  7.  * "License"); you may not use this file except in compliance
  8.  * with the License.  You may obtain a copy of the License at
  9.  *
  10.  * http://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  * Unless required by applicable law or agreed to in writing,
  13.  * software distributed under the License is distributed on an
  14.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15.  * KIND, either express or implied.  See the License for the
  16.  * specific language governing permissions and limitations
  17.  * under the License.
  18.  */
  19. package org.apache.commons.compress.changes;

  20. import java.io.InputStream;
  21. import java.util.Objects;

  22. import org.apache.commons.compress.archivers.ArchiveEntry;

  23. /**
  24.  * Change holds meta information about a change.
  25.  *
  26.  * @param <E> The ArchiveEntry type.
  27.  * @Immutable
  28.  */
  29. final class Change<E extends ArchiveEntry> {

  30.     /**
  31.      * Enumerates types of changes.
  32.      */
  33.     enum ChangeType {

  34.         /**
  35.          * Delete.
  36.          */
  37.         DELETE,

  38.         /**
  39.          * Add.
  40.          */
  41.         ADD,

  42.         /**
  43.          * Not used.
  44.          */
  45.         MOVE,

  46.         /**
  47.          * Delete directory.
  48.          */
  49.         DELETE_DIR
  50.     }

  51.     /** Entry name to delete. */
  52.     private final String targetFileName;

  53.     /** New entry to add. */
  54.     private final E entry;

  55.     /** Source for new entry. */
  56.     private final InputStream inputStream;

  57.     /** Change should replaceMode existing entries. */
  58.     private final boolean replaceMode;

  59.     /** Type of change. */
  60.     private final ChangeType type;

  61.     /**
  62.      * Constructs a change which adds an entry.
  63.      *
  64.      * @param archiveEntry the entry details
  65.      * @param inputStream  the InputStream for the entry data
  66.      */
  67.     Change(final E archiveEntry, final InputStream inputStream, final boolean replace) {
  68.         this.entry = Objects.requireNonNull(archiveEntry, "archiveEntry");
  69.         this.inputStream = Objects.requireNonNull(inputStream, "inputStream");
  70.         this.type = ChangeType.ADD;
  71.         this.targetFileName = null;
  72.         this.replaceMode = replace;
  73.     }

  74.     /**
  75.      * Constructs a new instance. Takes the file name of the file to be deleted from the stream as argument.
  76.      *
  77.      * @param fileName the file name of the file to delete
  78.      */
  79.     Change(final String fileName, final ChangeType type) {
  80.         this.targetFileName = Objects.requireNonNull(fileName, "fileName");
  81.         this.type = type;
  82.         this.inputStream = null;
  83.         this.entry = null;
  84.         this.replaceMode = true;
  85.     }

  86.     E getEntry() {
  87.         return entry;
  88.     }

  89.     InputStream getInputStream() {
  90.         return inputStream;
  91.     }

  92.     String getTargetFileName() {
  93.         return targetFileName;
  94.     }

  95.     ChangeType getType() {
  96.         return type;
  97.     }

  98.     boolean isReplaceMode() {
  99.         return replaceMode;
  100.     }
  101. }