• 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.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 
FileInfo(File file)33     public FileInfo(File file) {
34         name = file.getName();
35         path = file.getPath();
36         lastModifiedTime = file.lastModified();
37     }
38 
39     /**
40      * 获取文件路径
41      *
42      * @return 文件路径
43      */
getPath()44     public String getPath() {
45         return path;
46     }
47 
48     /**
49      * 重写比较方法,文件名和最后修改时间都相同才认为相等(被修改覆盖过的文件也认为是新文件)
50      *
51      * @param obj 待比较的文件对象
52      * @return 是否为相同文件
53      */
54     @Override
equals(Object obj)55     public boolean equals(Object obj) {
56         if (this == obj) {
57             return true;
58         }
59         if (obj == null || getClass() != obj.getClass()) {
60             return false;
61         }
62         FileInfo fileInfo = (FileInfo) obj;
63         return lastModifiedTime == fileInfo.lastModifiedTime && Objects.equals(name, fileInfo.name)
64                 && Objects.equals(path, fileInfo.path);
65     }
66 
67     @Override
hashCode()68     public int hashCode() {
69         return Objects.hash(name, path, lastModifiedTime);
70     }
71 }
72