aboutsummaryrefslogtreecommitdiff
path: root/car-lib/src/android/car/storagemonitoring/WearEstimate.java
blob: 507e6edff18874a05a839891ad353a8f8358cead (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package android.car.storagemonitoring;

import android.annotation.IntRange;
import android.annotation.SystemApi;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.JsonReader;
import android.util.JsonWriter;
import java.io.IOException;
import java.util.Objects;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Wear-out information for flash storage.
 *
 * Contains a lower-bound estimate of the wear of "type A" (SLC) and "type B" (MLC) storage.
 *
 * Current technology offers wear data in increments of 10% (i.e. from 0 == new device up to
 * 100 == worn-out device). It is possible for a storage device to only support one type of memory
 * cell, in which case it is expected that the storage type not supported will have UNKNOWN wear.
 *
 * @hide
 */
@SystemApi
public class WearEstimate implements Parcelable {
    public static final int UNKNOWN = -1;

    /** @hide */
    public static final WearEstimate UNKNOWN_ESTIMATE = new WearEstimate(UNKNOWN, UNKNOWN);

    public static final Parcelable.Creator<WearEstimate> CREATOR =
        new Parcelable.Creator<WearEstimate>() {
            public WearEstimate createFromParcel(Parcel in) {
                return new WearEstimate(in);
            }

            public WearEstimate[] newArray(int size) {
                return new WearEstimate[size];
            }
        };

    /**
     * Wear estimate data for "type A" storage.
     */
    @IntRange(from=-1, to=100)
    public final int typeA;

    /**
     * Wear estimate data for "type B" storage.
     */
    @IntRange(from=-1, to=100)
    public final int typeB;

    private static final int validateWearValue(int value) {
        if (value == UNKNOWN) return value;
        if ((value >= 0) && (value <= 100)) return value;
        throw new IllegalArgumentException(value + " is not a valid wear estimate");
    }

    public WearEstimate(int typeA, int typeB) {
        this.typeA = validateWearValue(typeA);
        this.typeB = validateWearValue(typeB);
    }

    public WearEstimate(Parcel in) {
        typeA = validateWearValue(in.readInt());
        typeB = validateWearValue(in.readInt());
    }

    public WearEstimate(JsonReader in) throws IOException {
        int typeA = UNKNOWN;
        int typeB = UNKNOWN;
        in.beginObject();
        while (in.hasNext()) {
            switch (in.nextName()) {
                case "wearEstimateTypeA":
                    typeA = validateWearValue(in.nextInt());
                    break;
                case "wearEstimateTypeB":
                    typeB = validateWearValue(in.nextInt());
                    break;
            }
        }
        in.endObject();
        this.typeA = typeA;
        this.typeB = typeB;
    }

    /** @hide */
    public WearEstimate(JSONObject in) throws JSONException {
        typeA = in.getInt("wearEstimateTypeA");
        typeB = in.getInt("wearEstimateTypeB");
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(typeA);
        dest.writeInt(typeB);
    }

    public void writeToJson(JsonWriter jsonWriter) throws IOException {
        jsonWriter.beginObject();
        jsonWriter.name("wearEstimateTypeA").value(typeA);
        jsonWriter.name("wearEstimateTypeB").value(typeB);
        jsonWriter.endObject();
    }

    @Override
    public boolean equals(Object other) {
        if (other instanceof WearEstimate) {
            WearEstimate wo = (WearEstimate)other;
            return wo.typeA == typeA && wo.typeB == typeB;
        }
        return false;
    }

    @Override
    public int hashCode() {
        return Objects.hash(typeA, typeB);
    }

    private static final String wearValueToString(int value) {
        if (value == UNKNOWN) return "unknown";
        return value + "%";
    }

    @Override
    public String toString() {
        return "type A: " + wearValueToString(typeA) + ", type B: " + wearValueToString(typeB);
    }
}