1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one *
3 * or more contributor license agreements. See the NOTICE file *
4 * distributed with this work for additional information *
5 * regarding copyright ownership. The ASF licenses this file *
6 * to you under the Apache License, Version 2.0 (the *
7 * "License"); you may not use this file except in compliance *
8 * with the License. You may obtain a copy of the License at *
9 * *
10 * http://www.apache.org/licenses/LICENSE-2.0 *
11 * *
12 * Unless required by applicable law or agreed to in writing, *
13 * software distributed under the License is distributed on an *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15 * KIND, either express or implied. See the License for the *
16 * specific language governing permissions and limitations *
17 * under the License. *
18 */
19 package org.apache.rat;
20
21 /**
22 * A formatter for Package information about a class.
23 * @see Package
24 */
25 public final class VersionInfo {
26 /**
27 * The version info string.
28 */
29 private final Package pkg;
30
31 /**
32 * Simple testing output. Prints values from default constructor.
33 * @param args not used.
34 */
35 public static void main(final String[] args) {
36 VersionInfo versionInfo = new VersionInfo();
37 System.out.println(versionInfo);
38 System.out.println("title: " + versionInfo.getTitle());
39 System.out.println("version: " + versionInfo.getVersion());
40 System.out.println("vendor: " + versionInfo.getVendor());
41 System.out.println("spec title: " + versionInfo.getSpecTitle());
42 System.out.println("spec version: " + versionInfo.getSpecVersion());
43 System.out.println("spec vendor: " + versionInfo.getSpecVendor());
44 }
45
46 private String orDefault(final String value, final String defaultValue) {
47 return value == null ? defaultValue : value;
48 }
49
50 /**
51 * Constructor that uses the VersionInfo package for information.
52 */
53 public VersionInfo() {
54 this(VersionInfo.class);
55 }
56
57 /**
58 * Constructor for a specific class.
59 * @param clazz the class to get the Package information from.
60 */
61 public VersionInfo(final Class<?> clazz) {
62 pkg = clazz.getPackage();
63 }
64
65 /**
66 * Default string representation of the implementation information from the package.
67 * @return The string representation.
68 */
69 @Override
70 public String toString() {
71 return String.format("%s %s (%s)", getTitle(), getVersion(), getVendor());
72 }
73
74 /**
75 * Gets the implementation version of the package. Will return "VERSION-NUMBER" if
76 * package information is not available.
77 * @return the implementation version.
78 */
79 public String getVersion() {
80 return orDefault(pkg.getImplementationVersion(), "VERSION-NUMBER");
81 }
82
83 /**
84 * Gets the implementation vendor of the package. Will return "VENDOR-NAME" if
85 * package information is not available.
86 * @return the implementation vendor
87 */
88 public String getVendor() {
89 return orDefault(pkg.getImplementationVendor(), "VENDOR-NAME");
90 }
91
92 /**
93 * Gets the implementation title of the package. Will return "TITLE" if
94 * package information is not available.
95 * @return the implementation title
96 */
97 public String getTitle() {
98 return orDefault(pkg.getImplementationTitle(), "TITLE");
99 }
100
101 /**
102 * Gets the specification version of the package. Will return "SPEC-VERSION" if
103 * package information is not available.
104 * @return the specification version.
105 */
106 public String getSpecVersion() {
107 return orDefault(pkg.getSpecificationVersion(), "SPEC-VERSION");
108 }
109
110 /**
111 * Gets the specification vendor of the package. Will return "SPEC-VENDOR" if
112 * package information is not available.
113 * @return the specification vendor
114 */
115 public String getSpecVendor() {
116 return orDefault(pkg.getSpecificationVendor(), "SPEC-VENDOR");
117 }
118
119 /**
120 * Gets the specification title of the package. Will return "SPEC-TITLE" if
121 * package information is not available.
122 * @return the specification title
123 */
124 public String getSpecTitle() {
125 return orDefault(pkg.getSpecificationTitle(), "SPEC-TITLE");
126 }
127 }