• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 package com.sk.ts.utils;
16 
17 import java.io.File;
18 import java.util.Objects;
19 
20 /**
21  * 文件信息类,提供自定义的文件对象比较方法
22  *
23  * @author: zhangzhicheng
24  * @see: generator dialog
25  * @version: v1.0.0
26  * @since 2023-01-18
27  */
28 public class FileInfo {
29     private String name;
30     private String path;
31     private long lastModifiedTime;
32     private boolean isTimeEqual = false;
33     private boolean isNameEqual = false;
34     private boolean isPathEqual = false;
35 
FileInfo(File file)36     public FileInfo(File file) {
37         name = file.getName();
38         path = file.getPath();
39         lastModifiedTime = file.lastModified();
40     }
41 
42     /**
43      * 获取文件路径
44      *
45      * @return 文件路径
46      */
getPath()47     public String getPath() {
48         return path;
49     }
50 
51     /**
52      * 重写比较方法,文件名和最后修改时间都相同才认为相等(被修改覆盖过的文件也认为是新文件)
53      *
54      * @param object 待比较的文件对象
55      * @return 是否为相同文件
56      */
57     @Override
equals(Object object)58     public boolean equals(Object object) {
59         FileInfo info = (FileInfo) object;
60         if (lastModifiedTime == info.lastModifiedTime) {
61             isTimeEqual = true;
62         }
63         if (Objects.equals(name, info.name)) {
64             isNameEqual = true;
65         }
66         if (Objects.equals(path, info.path)) {
67             isPathEqual = true;
68         }
69         if (object == null || getClass() != object.getClass()) {
70             return false;
71         }
72         if (this == object) {
73             return true;
74         }
75         return isTimeEqual && isNameEqual && isPathEqual;
76     }
77 
78     @Override
hashCode()79     public int hashCode() {
80         return Objects.hash(name, path, lastModifiedTime);
81     }
82 }
83