View Javadoc
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  
21  import java.io.InputStream;
22  import java.util.Objects;
23  
24  import org.apache.commons.compress.archivers.ArchiveEntry;
25  
26  /**
27   * Change holds meta information about a change.
28   *
29   * @param <E> The ArchiveEntry type.
30   * @Immutable
31   */
32  final class Change<E extends ArchiveEntry> {
33  
34      /**
35       * Enumerates types of changes.
36       */
37      enum ChangeType {
38  
39          /**
40           * Delete.
41           */
42          DELETE,
43  
44          /**
45           * Add.
46           */
47          ADD,
48  
49          /**
50           * Not used.
51           */
52          MOVE,
53  
54          /**
55           * Delete directory.
56           */
57          DELETE_DIR
58      }
59  
60      /** Entry name to delete. */
61      private final String targetFileName;
62  
63      /** New entry to add. */
64      private final E entry;
65  
66      /** Source for new entry. */
67      private final InputStream inputStream;
68  
69      /** Change should replaceMode existing entries. */
70      private final boolean replaceMode;
71  
72      /** Type of change. */
73      private final ChangeType type;
74  
75      /**
76       * Constructs a change which adds an entry.
77       *
78       * @param archiveEntry the entry details
79       * @param inputStream  the InputStream for the entry data
80       */
81      Change(final E archiveEntry, final InputStream inputStream, final boolean replace) {
82          this.entry = Objects.requireNonNull(archiveEntry, "archiveEntry");
83          this.inputStream = Objects.requireNonNull(inputStream, "inputStream");
84          this.type = ChangeType.ADD;
85          this.targetFileName = null;
86          this.replaceMode = replace;
87      }
88  
89      /**
90       * Constructs a new instance. Takes the file name of the file to be deleted from the stream as argument.
91       *
92       * @param fileName the file name of the file to delete
93       */
94      Change(final String fileName, final ChangeType type) {
95          this.targetFileName = Objects.requireNonNull(fileName, "fileName");
96          this.type = type;
97          this.inputStream = null;
98          this.entry = null;
99          this.replaceMode = true;
100     }
101 
102     E getEntry() {
103         return entry;
104     }
105 
106     InputStream getInputStream() {
107         return inputStream;
108     }
109 
110     String getTargetFileName() {
111         return targetFileName;
112     }
113 
114     ChangeType getType() {
115         return type;
116     }
117 
118     boolean isReplaceMode() {
119         return replaceMode;
120     }
121 }