View Javadoc
1   /*
2    * Copyright 2025 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.software.os;
6   
7   import java.util.Collections;
8   import java.util.LinkedHashMap;
9   import java.util.Map;
10  import java.util.Objects;
11  
12  /**
13   * Represents common information about an installed application across different operating systems. This class provides
14   * standardized access to essential application details while allowing flexibility for OS-specific fields via an
15   * additional information map.
16   */
17  public class ApplicationInfo {
18  
19      /** The name of the application. */
20      private final String name;
21      /** The version of the application. */
22      private final String version;
23      /** The vendor or publisher of the application. */
24      private final String vendor;
25      /**
26       * The installation or last modified timestamp of the application in milliseconds since epoch. This represents the
27       * Unix timestamp.
28       */
29      private final long timestamp;
30      /**
31       * A map containing additional application details such as install location, source, etc. Keys are field names, and
32       * values are corresponding details.
33       */
34      private final Map<String, String> additionalInfo;
35  
36      /**
37       * Constructs an {@code ApplicationInfo} object with the specified details.
38       *
39       * @param name           The name of the application.
40       * @param version        The version of the application.
41       * @param vendor         The vendor or publisher of the application.
42       * @param timestamp      The installation or last modified timestamp in milliseconds since epoch.
43       * @param additionalInfo A map of additional information (can be {@code null}, in which case an empty map is used).
44       */
45      public ApplicationInfo(String name, String version, String vendor, long timestamp,
46              Map<String, String> additionalInfo) {
47          this.name = name;
48          this.version = version;
49          this.vendor = vendor;
50          this.timestamp = timestamp;
51          this.additionalInfo = additionalInfo != null ? new LinkedHashMap<>(additionalInfo) : Collections.emptyMap();
52      }
53  
54      /**
55       * Gets the name of the installed application.
56       *
57       * @return The application name, or an empty string if not available.
58       */
59      public String getName() {
60          return name;
61      }
62  
63      /**
64       * Gets the version of the installed application.
65       *
66       * @return The application version, or an empty string if not available.
67       */
68      public String getVersion() {
69          return version;
70      }
71  
72      /**
73       * Gets the vendor or publisher of the installed application.
74       *
75       * @return The vendor name, or an empty string if not available.
76       */
77      public String getVendor() {
78          return vendor;
79      }
80  
81      /**
82       * Gets the last modified or installation time of the application. The timestamp is represented in milliseconds
83       * since the Unix epoch (January 1, 1970, UTC).
84       * <p>
85       * - On Windows, this corresponds to the application's install date. - On Linux, it represents the package's
86       * installation or last modified time. - On macOS, it reflects the last modification timestamp of the application
87       * bundle.
88       * </p>
89       *
90       * @return The last modified time in epoch milliseconds, or {@code 0} if unavailable.
91       */
92      public long getTimestamp() {
93          return timestamp;
94      }
95  
96      /**
97       * Gets additional application details that are OS-specific and not covered by the main fields. This map may include
98       * attributes like installation location, source, architecture, or other metadata.
99       *
100      * @return A map containing optional key-value pairs of application details.
101      */
102     public Map<String, String> getAdditionalInfo() {
103         return additionalInfo;
104     }
105 
106     @Override
107     public String toString() {
108         return "AppInfo{" + "name=" + name + ", version=" + version + ", vendor=" + vendor + ", timestamp=" + timestamp
109                 + ", additionalInfo=" + additionalInfo + '}';
110     }
111 
112     @Override
113     public boolean equals(Object o) {
114         if (this == o) {
115             return true;
116         }
117         if (!(o instanceof ApplicationInfo)) {
118             return false;
119         }
120         ApplicationInfo that = (ApplicationInfo) o;
121         return timestamp == that.timestamp && Objects.equals(name, that.name) && Objects.equals(version, that.version)
122                 && Objects.equals(vendor, that.vendor) && Objects.equals(additionalInfo, that.additionalInfo);
123     }
124 
125     @Override
126     public int hashCode() {
127         return Objects.hash(name, version, vendor, timestamp, additionalInfo);
128     }
129 }