1 /*
2  * Copyright (C) 2017 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 com.android.car.storagemonitoring;
17 
18 import static java.util.Objects.hash;
19 
20 import android.car.storagemonitoring.WearEstimate;
21 import java.time.Instant;
22 
23 public final class WearInformation {
24     public static final int UNKNOWN_LIFETIME_ESTIMATE = -1;
25 
26     public static final int UNKNOWN_PRE_EOL_INFO = 0;
27     public static final int PRE_EOL_INFO_NORMAL = 1;
28     public static final int PRE_EOL_INFO_WARNING = 2;
29     public static final int PRE_EOL_INFO_URGENT = 3;
30 
31     private static final String UNKNOWN = "unknown";
32     private static final String[] PRE_EOL_STRINGS = new String[] {
33         UNKNOWN, "normal", "warning", "urgent"
34     };
35 
36     /**
37      * A lower bound on the lifetime estimate for "type A" memory cells, expressed as a percentage.
38      */
39     public final int lifetimeEstimateA;
40 
41     /**
42      * A lower bound on the lifetime estimate for "type B" memory cells, expressed as a percentage.
43      */
44     public final int lifetimeEstimateB;
45 
46     /**
47      * An estimate of the lifetime based on reserved block consumption.
48      */
49     public final int preEolInfo;
50 
WearInformation(int lifetimeA, int lifetimeB, int preEol)51     public WearInformation(int lifetimeA, int lifetimeB, int preEol) {
52         lifetimeEstimateA = lifetimeA;
53         lifetimeEstimateB = lifetimeB;
54         preEolInfo = preEol;
55     }
56 
57     @Override
hashCode()58     public int hashCode() {
59         return hash(lifetimeEstimateA, lifetimeEstimateB, preEolInfo);
60     }
61 
62     @Override
equals(Object other)63     public boolean equals(Object other) {
64         if (other instanceof WearInformation) {
65             WearInformation wi = (WearInformation)other;
66             return (wi.lifetimeEstimateA == lifetimeEstimateA) &&
67                 (wi.lifetimeEstimateB == lifetimeEstimateB) &&
68                 (wi.preEolInfo == preEolInfo);
69         } else {
70             return false;
71         }
72     }
73 
lifetimeToString(int lifetime)74     private String lifetimeToString(int lifetime) {
75         if (lifetime == UNKNOWN_LIFETIME_ESTIMATE) return UNKNOWN;
76 
77         return lifetime + "%";
78     }
79 
80     @Override
toString()81     public String toString() {
82         return String.format("lifetime estimate: A = %s, B = %s; pre EOL info: %s",
83                 lifetimeToString(lifetimeEstimateA),
84                 lifetimeToString(lifetimeEstimateB),
85                 PRE_EOL_STRINGS[preEolInfo]);
86     }
87 
toWearEstimate()88     public WearEstimate toWearEstimate() {
89         return new WearEstimate(lifetimeEstimateA, lifetimeEstimateB);
90     }
91 }
92