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