• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package android.car.storagemonitoring;
17 
18 import static com.android.car.internal.ExcludeFromCodeCoverageGeneratedReport.BOILERPLATE_CODE;
19 
20 import android.annotation.SystemApi;
21 import android.os.Parcel;
22 import android.os.Parcelable;
23 import android.util.JsonWriter;
24 
25 import com.android.car.internal.ExcludeFromCodeCoverageGeneratedReport;
26 
27 import org.json.JSONException;
28 import org.json.JSONObject;
29 
30 import java.io.IOException;
31 import java.util.Objects;
32 
33 /**
34  * Information about how many bytes were written to a filesystem during its lifetime.
35  *
36  * @hide
37  */
38 @SystemApi
39 public final class LifetimeWriteInfo implements Parcelable {
40     public static final Creator<LifetimeWriteInfo> CREATOR = new Creator<LifetimeWriteInfo>() {
41         @Override
42         public LifetimeWriteInfo createFromParcel(Parcel in) {
43             return new LifetimeWriteInfo(in);
44         }
45 
46         @Override
47         public LifetimeWriteInfo[] newArray(int size) {
48             return new LifetimeWriteInfo[size];
49         }
50     };
51 
52     public final String partition;
53     public final String fstype;
54     public final long writtenBytes;
55 
LifetimeWriteInfo(String partition, String fstype, long writtenBytes)56     public LifetimeWriteInfo(String partition, String fstype, long writtenBytes) {
57         this.partition = Objects.requireNonNull(partition);
58         this.fstype = Objects.requireNonNull(fstype);
59         if (writtenBytes < 0) {
60             throw new IllegalArgumentException("writtenBytes must be non-negative");
61         }
62         this.writtenBytes = writtenBytes;
63     }
64 
LifetimeWriteInfo(Parcel in)65     public LifetimeWriteInfo(Parcel in) {
66         this.partition = in.readString();
67         this.fstype = in.readString();
68         this.writtenBytes = in.readLong();
69     }
70 
71     /**
72      * @hide
73      */
LifetimeWriteInfo(JSONObject in)74     public LifetimeWriteInfo(JSONObject in) throws JSONException {
75         partition = in.getString("partition");
76         fstype = in.getString("fstype");
77         writtenBytes = in.getLong("writtenBytes");
78     }
79 
80 
81     @Override
writeToParcel(Parcel dest, int flags)82     public void writeToParcel(Parcel dest, int flags) {
83         dest.writeString(partition);
84         dest.writeString(fstype);
85         dest.writeLong(writtenBytes);
86     }
87 
88     /**
89      * @hide
90      */
writeToJson(JsonWriter jsonWriter)91     public void writeToJson(JsonWriter jsonWriter) throws IOException {
92         jsonWriter.beginObject();
93         jsonWriter.name("partition").value(partition);
94         jsonWriter.name("fstype").value(fstype);
95         jsonWriter.name("writtenBytes").value(writtenBytes);
96         jsonWriter.endObject();
97     }
98 
99 
100     @Override
101     @ExcludeFromCodeCoverageGeneratedReport(reason = BOILERPLATE_CODE)
describeContents()102     public int describeContents() {
103         return 0;
104     }
105 
106     @Override
equals(Object other)107     public boolean equals(Object other) {
108         if (other instanceof LifetimeWriteInfo) {
109             LifetimeWriteInfo lifetime = (LifetimeWriteInfo) other;
110             return partition.equals(lifetime.partition)
111                     && fstype.equals(lifetime.fstype)
112                     && writtenBytes == lifetime.writtenBytes;
113         }
114 
115         return false;
116     }
117 
118     @Override
hashCode()119     public int hashCode() {
120         return Objects.hash(partition, fstype, writtenBytes);
121     }
122 
123     @Override
toString()124     public String toString() {
125         return String.format("for partition %s of type %s, %d bytes were written",
126                 partition, fstype, writtenBytes);
127     }
128 }
129