View Javadoc
1   /*
2    * This file is part of Waarp Project (named also Waarp or GG).
3    *
4    *  Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
5    *  tags. See the COPYRIGHT.txt in the distribution for a full listing of
6    * individual contributors.
7    *
8    *  All Waarp Project is free software: you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or (at your
11   * option) any later version.
12   *
13   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License along with
18   * Waarp . If not, see <http://www.gnu.org/licenses/>.
19   */
20  package org.waarp.snmp.utils;
21  
22  import org.waarp.snmp.interf.WaarpGauge32;
23  
24  /**
25   * Specific Value for Gauge32 for Memory usage
26   */
27  public class MemoryGauge32 extends WaarpGauge32 {
28    /**
29     *
30     */
31    private static final long serialVersionUID = -435321381525820545L;
32  
33    /**
34     * The different Type of Memory Gauge32 elements
35     */
36    public enum MemoryType {
37      TotalMemory, FreeMemory, UsedMemory
38    }
39  
40    /**
41     * Runtime for Memory
42     */
43    private transient final Runtime runtime = Runtime.getRuntime();
44    /**
45     * Type of MemoryType used
46     */
47    protected final MemoryType type;
48  
49    @Override
50    protected final void setInternalValue() {
51      if (type == null) {
52        return;
53      }
54      final long mem;
55      switch (type) {
56        case TotalMemory:
57          mem = runtime.totalMemory();
58          setValue(mem >> 10);
59          return;
60        case FreeMemory:
61          mem = runtime.freeMemory();
62          setValue(mem >> 10);
63          return;
64        case UsedMemory:
65          mem = runtime.totalMemory() - runtime.freeMemory();
66          setValue(mem >> 10);
67      }
68    }
69  
70    @Override
71    protected final void setInternalValue(final long value) {
72      // ignored
73      setInternalValue();
74    }
75  
76    /**
77     * @param type the type of MemoryType used
78     */
79    public MemoryGauge32(final MemoryType type) {
80      this.type = type;
81      setInternalValue();
82    }
83  }