View Javadoc
1   /*
2    * Copyright 2020-2022 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.hardware.common;
6   
7   import oshi.annotation.concurrent.ThreadSafe;
8   import oshi.hardware.HWDiskStore;
9   import oshi.util.FormatUtil;
10  
11  /**
12   * Common methods for platform HWDiskStore classes
13   */
14  @ThreadSafe
15  public abstract class AbstractHWDiskStore implements HWDiskStore {
16  
17      private final String name;
18      private final String model;
19      private final String serial;
20      private final long size;
21  
22      protected AbstractHWDiskStore(String name, String model, String serial, long size) {
23          this.name = name;
24          this.model = model;
25          this.serial = serial;
26          this.size = size;
27      }
28  
29      @Override
30      public String getName() {
31          return this.name;
32      }
33  
34      @Override
35      public String getModel() {
36          return this.model;
37      }
38  
39      @Override
40      public String getSerial() {
41          return this.serial;
42      }
43  
44      @Override
45      public long getSize() {
46          return this.size;
47      }
48  
49      @Override
50      public String toString() {
51          boolean readwrite = getReads() > 0 || getWrites() > 0;
52          StringBuilder sb = new StringBuilder();
53          sb.append(getName()).append(": ");
54          sb.append("(model: ").append(getModel());
55          sb.append(" - S/N: ").append(getSerial()).append(") ");
56          sb.append("size: ").append(getSize() > 0 ? FormatUtil.formatBytesDecimal(getSize()) : "?").append(", ");
57          sb.append("reads: ").append(readwrite ? getReads() : "?");
58          sb.append(" (").append(readwrite ? FormatUtil.formatBytes(getReadBytes()) : "?").append("), ");
59          sb.append("writes: ").append(readwrite ? getWrites() : "?");
60          sb.append(" (").append(readwrite ? FormatUtil.formatBytes(getWriteBytes()) : "?").append("), ");
61          sb.append("xfer: ").append(readwrite ? getTransferTime() : "?");
62          return sb.toString();
63      }
64  }