View Javadoc
1   /*
2    * Copyright 2016-2022 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.hardware.common;
6   
7   import static oshi.util.Memoizer.memoize;
8   
9   import java.util.List;
10  import java.util.function.Supplier;
11  
12  import oshi.annotation.concurrent.ThreadSafe;
13  import oshi.hardware.CentralProcessor;
14  import oshi.hardware.ComputerSystem;
15  import oshi.hardware.GlobalMemory;
16  import oshi.hardware.HardwareAbstractionLayer;
17  import oshi.hardware.NetworkIF;
18  import oshi.hardware.Sensors;
19  
20  /**
21   * Common fields or methods used by platform-specific implementations of HardwareAbstractionLayer
22   */
23  @ThreadSafe
24  public abstract class AbstractHardwareAbstractionLayer implements HardwareAbstractionLayer {
25  
26      private final Supplier<ComputerSystem> computerSystem = memoize(this::createComputerSystem);
27  
28      private final Supplier<CentralProcessor> processor = memoize(this::createProcessor);
29  
30      private final Supplier<GlobalMemory> memory = memoize(this::createMemory);
31  
32      private final Supplier<Sensors> sensors = memoize(this::createSensors);
33  
34      @Override
35      public ComputerSystem getComputerSystem() {
36          return computerSystem.get();
37      }
38  
39      /**
40       * Instantiates the platform-specific {@link ComputerSystem} object
41       *
42       * @return platform-specific {@link ComputerSystem} object
43       */
44      protected abstract ComputerSystem createComputerSystem();
45  
46      @Override
47      public CentralProcessor getProcessor() {
48          return processor.get();
49      }
50  
51      /**
52       * Instantiates the platform-specific {@link CentralProcessor} object
53       *
54       * @return platform-specific {@link CentralProcessor} object
55       */
56      protected abstract CentralProcessor createProcessor();
57  
58      @Override
59      public GlobalMemory getMemory() {
60          return memory.get();
61      }
62  
63      /**
64       * Instantiates the platform-specific {@link GlobalMemory} object
65       *
66       * @return platform-specific {@link GlobalMemory} object
67       */
68      protected abstract GlobalMemory createMemory();
69  
70      @Override
71      public Sensors getSensors() {
72          return sensors.get();
73      }
74  
75      /**
76       * Instantiates the platform-specific {@link Sensors} object
77       *
78       * @return platform-specific {@link Sensors} object
79       */
80      protected abstract Sensors createSensors();
81  
82      @Override
83      public List<NetworkIF> getNetworkIFs() {
84          return getNetworkIFs(false);
85      }
86  }