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 android.annotation.SystemApi;
19 import android.os.Parcel;
20 import android.os.Parcelable;
21 import android.util.JsonWriter;
22 
23 import org.json.JSONException;
24 import org.json.JSONObject;
25 
26 import java.io.IOException;
27 import java.util.Objects;
28 
29 /**
30  * Information about how many bytes were written to a filesystem during its lifetime.
31  *
32  * @hide
33  */
34 @SystemApi
35 public final class LifetimeWriteInfo implements Parcelable {
36     public static final Creator<IoStats> CREATOR = new Creator<IoStats>() {
37         @Override
38         public IoStats createFromParcel(Parcel in) {
39             return new IoStats(in);
40         }
41 
42         @Override
43         public IoStats[] newArray(int size) {
44             return new IoStats[size];
45         }
46     };
47 
48     public final String partition;
49     public final String fstype;
50     public final long writtenBytes;
51 
LifetimeWriteInfo(String partition, String fstype, long writtenBytes)52     public LifetimeWriteInfo(String partition, String fstype, long writtenBytes) {
53         this.partition = Objects.requireNonNull(partition);
54         this.fstype = Objects.requireNonNull(fstype);
55         if (writtenBytes < 0) {
56             throw new IllegalArgumentException("writtenBytes must be non-negative");
57         }
58         this.writtenBytes = writtenBytes;
59     }
60 
LifetimeWriteInfo(Parcel in)61     public LifetimeWriteInfo(Parcel in) {
62         this.partition = in.readString();
63         this.fstype = in.readString();
64         this.writtenBytes = in.readLong();
65     }
66 
67     /**
68      * @hide
69      */
LifetimeWriteInfo(JSONObject in)70     public LifetimeWriteInfo(JSONObject in) throws JSONException {
71         partition = in.getString("partition");
72         fstype = in.getString("fstype");
73         writtenBytes = in.getLong("writtenBytes");
74     }
75 
76 
77     @Override
writeToParcel(Parcel dest, int flags)78     public void writeToParcel(Parcel dest, int flags) {
79         dest.writeString(partition);
80         dest.writeString(fstype);
81         dest.writeLong(writtenBytes);
82     }
83 
84     /**
85      * @hide
86      */
writeToJson(JsonWriter jsonWriter)87     public void writeToJson(JsonWriter jsonWriter) throws IOException {
88         jsonWriter.beginObject();
89         jsonWriter.name("partition").value(partition);
90         jsonWriter.name("fstype").value(fstype);
91         jsonWriter.name("writtenBytes").value(writtenBytes);
92         jsonWriter.endObject();
93     }
94 
95 
96     @Override
describeContents()97     public int describeContents() {
98         return 0;
99     }
100 
101     @Override
equals(Object other)102     public boolean equals(Object other) {
103         if (other instanceof LifetimeWriteInfo) {
104             LifetimeWriteInfo lifetime = (LifetimeWriteInfo) other;
105             return partition.equals(lifetime.partition)
106                     && fstype.equals(lifetime.fstype)
107                     && writtenBytes == lifetime.writtenBytes;
108         }
109 
110         return false;
111     }
112 
113     @Override
toString()114     public String toString() {
115         return String.format("for partition %s of type %s, %d bytes were written",
116                 partition, fstype, writtenBytes);
117     }
118 }
119