001/*
002 * Copyright (c) 2009 The openGion Project.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
013 * either express or implied. See the License for the specific language
014 * governing permissions and limitations under the License.
015 */
016package org.opengion.fukurou.model;
017
018import java.io.File;
019import java.io.FileFilter;
020import java.io.FileNotFoundException;
021import java.io.IOException;
022import java.io.InputStream;
023
024/**
025 * CloudFileOperation用のファイル情報の格納クラス
026 *
027 * listFilesで取得した、ディレクトリとファイル一覧情報を格納します。
028 *
029 * パフォーマンスや分かりやすさを考慮してCloudFileOperationからは分離しています
030 *
031 * @og.group ファイル操作
032 *
033 * @og.rev 5.10.8.0 (2019/02/01) 新規作成
034 * @og.rev 5.10.9.0 (2019/03/01) 変更対応
035 * @author oota
036 * @since JDK7.0
037 */
038public class FileOperationInfo extends CloudFileOperation {
039        //* このプログラムのVERSION文字列を設定します。{@VALUE} */
040        private static final String VERSION = "8.0.0.0 (2021/07/31)" ;
041        private static final long serialVersionUID = 800020210731L ;
042
043        /** クラス変数 */
044        private final String plugin;
045
046        private long size;
047        // 8.0.0.0 (2021/07/31) Field ** has the same name as a method
048//      private long lastModified;
049//      private boolean isFile;
050//      private boolean isDirectory;
051        private long lastTime;                          // 最終時刻
052        private boolean isFil;                          // ファイルか?
053        private boolean isDir;                          // フォルダか?
054        private FileOperation file;                     // ファイルオペレータ
055
056        /**
057         * コンストラクタ
058         *
059         * 生成時の初期処理。
060         *
061         * @og.rev 8.0.0.0 (2021/07/31) Field ** has the same name as a method
062         *
063         * @param plugin プラグイン名
064         * @param bucket バケット名
065         * @param path ファイルパス
066         */
067        public FileOperationInfo(final String plugin, final String bucket, final String path) {
068                super(bucket, path);
069                this.plugin     = plugin;
070                size            = 0;
071//              lastModified = 0;
072//              isFile = false;
073//              isDirectory = false;
074                lastTime        = 0L;
075                isFil           = false;
076                isDir           = false;
077                file            = null;
078        }
079
080        /**
081         * FileOperationクラスの生成
082         *
083         * 呼び出し時に、FileOperationインスタンスが未生成の場合は、
084         * 生成を行います。
085         */
086        private void setFileOperation() {
087                if(file == null) {
088                        file = FileOperationFactory.newStorageOperation( plugin, conBucket, conPath );
089                }
090        }
091
092        /** Method */
093        /**
094         * 書き込み処理
095         *
096         * InputStreamのデータを書き込みます。
097         *
098         * @param is 書き込みデータのInputStream
099         * @throws IOException ファイル関連エラー情報
100         */
101        @Override
102        public void write(final InputStream is) throws IOException {
103                setFileOperation();
104                file.write(is);
105        }
106
107        /**
108         * 読み込み処理
109         *
110         * データを読み込み、InputStreamとして、返します。
111         *
112         * @return 読み込みデータのInputStream
113         * @throws FileNotFoundException ファイル非存在エラー情報
114         */
115        @Override
116        public InputStream read() throws FileNotFoundException {
117                setFileOperation();
118                return file.read();
119        }
120
121        /**
122         * 削除処理
123         *
124         * ファイルを削除します。
125         *
126         * @return 成否フラグ
127         */
128        @Override
129        public boolean delete() {
130                setFileOperation();
131                return file.delete();
132        }
133
134        /**
135         * コピー処理
136         *
137         * ファイルを指定先に、コピーします。
138         *
139         * @param afPath コピー先
140         * @return 成否フラグ
141         */
142        @Override
143        public boolean copy(final String afPath) {
144                setFileOperation();
145                return file.copy(afPath);
146        }
147
148        /**
149         * 一覧取得
150         *
151         * 1つ下の、ディレクトリ・ファイル一覧を取得します。
152         *
153         * @param filter フィルタ情報
154         * @return ファイル一覧
155         */
156        @Override
157        public File[] listFiles(final FileFilter filter) {
158                setFileOperation();
159                return file.listFiles(filter);
160        }
161
162        /**
163         * ファイルサイズ取得
164         *
165         * ファイルサイズを取得します。
166         *
167         * @return ファイルサイズ
168         */
169        @Override
170        public long length() {
171                return size;
172        }
173
174        /**
175         * ファイルサイズ設定
176         *
177         * ファイルサイズを設定します。
178         *
179         * @param size ファイルサイズ
180         */
181        public void setSize(final long size) {
182                this.size = size;
183        }
184
185        /**
186         * 最終更新時刻の取得
187         *
188         * 最終更新時刻を取得します。
189         *
190         * @og.rev 8.0.0.0 (2021/07/31) Field ** has the same name as a method
191         *
192         * @return 最終更新時刻
193         */
194        @Override
195        public long lastModified() {
196//              return lastModified;
197                return lastTime;
198        }
199
200        /**
201         * 最終更新時刻の設定
202         *
203         * 最終更新時刻を設定します。
204         *
205         * @og.rev 8.0.0.0 (2021/07/31) Field ** has the same name as a method
206         *
207         * @param lastModified 最終更新時刻
208         */
209        public void setLastModifiedValue(final long lastModified) {
210//              this.lastModified = lastModified;
211                lastTime = lastModified;
212        }
213
214        /**
215         * ファイル判定取得
216         *
217         * ファイルであるかの判定を返します。
218         *
219         * @og.rev 8.0.0.0 (2021/07/31) Field ** has the same name as a method
220         *
221         * @return ファイル判定
222         */
223        @Override
224        public boolean isFile() {
225//              return isFile;
226                return isFil;
227        }
228
229        /**
230         * ファイル判定設定
231         *
232         * ファイルであるかの判定を設定します。
233         *
234         * @og.rev 8.0.0.0 (2021/07/31) Field ** has the same name as a method
235         *
236         * @param isFile ファイル判定
237         */
238        public void setFile(final boolean isFile) {
239//              this.isFile = isFile;
240                isFil = isFile;
241        }
242
243        /**
244         * ディレクトリ判定取得
245         *
246         * ディレクトリであるかの判定を返します。
247         *
248         * @og.rev 8.0.0.0 (2021/07/31) Field ** has the same name as a method
249         *
250         * @return ディレクトリ判定
251         */
252        @Override
253        public boolean isDirectory() {
254//              return isDirectory;
255                return isDir;
256        }
257
258        /**
259         * ディレクトリ判定設定
260         *
261         * ディレクトリであるかの判定を設定します。
262         *
263         * @og.rev 8.0.0.0 (2021/07/31) Field ** has the same name as a method
264         *
265         * @param isDirectory ディレクトリ判定
266         */
267        public void setDirectory(final boolean isDirectory) {
268//              this.isDirectory = isDirectory;
269                isDir = isDirectory;
270        }
271
272        /**
273         * 親情報の取得
274         *
275         * 親情報を返します。
276         *
277         * @return 親情報
278         */
279        @Override
280        public File getParentFile() {
281                return  FileOperationFactory.newStorageOperation( file , this.getParent() );
282        }
283
284//      /**
285//       * このオブジェクトと他のオブジェクトが等しいかどうかを示します。
286//       * インタフェース Comparable の 実装に関連して、再定義しています。
287//       *
288//       * @og.rev 7.2.9.4 (2020/11/20) spotbugs:スーパークラスの equals メソッドをオーバーライドしていないクラス
289//       *
290//       * @param   object 比較対象の参照オブジェクト
291//       *
292//       * @return      引数に指定されたオブジェクトとこのオブジェクトが等しい場合は true、そうでない場合は false
293//       */
294//      @Override
295//      public boolean equals( final Object object ) {
296//              return object instanceof File && super.equals( object );
297//      }
298
299//      /**
300//       * オブジェクトのハッシュコード値を返します。
301//       * このメソッドは、java.io.File のハッシュ値を返すことで、equals メソッドとの整合性を取っています。
302//       *
303//       * @og.rev 7.2.9.4 (2020/11/20) spotbugs:equals メソッドは定義していますが hashCode メソッドは定義していないクラス
304//       * @og.rev 8.0.0.0 (2021/07/31) Overriding method merely calls super
305//       *
306//       * @return  このオブジェクトのハッシュコード値
307//       */
308//      @Override
309//      public int hashCode() {
310//              return super.hashCode() ;                               // PMD:Overriding method merely calls super が出る。多分定義不要
311//      }
312
313//      // テスト用メソッドです
314//      public static void main(String[] args) {
315//              System.out.println("start");
316//
317//              FileOperation file = new FileOperationInfo("aws", "otest20190205", "sample/test.txt");
318//
319//              File parent = file.getParentFile();
320//              System.out.println(parent.getPath());
321//              System.out.println(parent.isDirectory());
322//              System.out.println(parent.isFile());
323//
324//              System.out.println("end");
325//      }
326}