Debian Codegraph @

jna-rinchi-core/src/main/java/io/github/dan2097/jnarinchi/RinchiOptions.java

raw
1 /**
2 * JNA-RInChI - Library for calling RInChI from Java
3 * Copyright © 2022 Nikolay Kochev
4 * <p>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 * <p>
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 * <p>
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 package io.github.dan2097.jnarinchi;
19
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.EnumSet;
23 import java.util.List;
24 import java.util.Locale;
25
26
27 /**
28 * Holds available option settings to be used with RInChI operations.
29 * <br>
30 * The {@link #DEFAULT_OPTIONS} don't use any customized options for RInChI generation.
31 * <br>
32 * This object comes with a builder for configuration:
33 * <pre>
34 * RinchiFlag[] flags = new RinchiFlag[] {RinchiFlag.ForceEquilibrium};
35 * RinchiOptions options = RinchiOptions.builder().withFlags(flags).build();
36 * </pre>
37 *
38 * @author Nikolay Kochev
39 */
40 public class RinchiOptions {
41 /**
42 * RinchiOption object configured with default settings.
43 */
44 public static final RinchiOptions DEFAULT_OPTIONS = RinchiOptions.builder().build();
45 private static final boolean IS_WINDOWS = System.getProperty("os.name", "").toLowerCase(Locale.ROOT).startsWith("windows");
46
47 private final List<RinchiFlag> flags;
48
49 private RinchiOptions(RinchiOptionsBuilder builder) {
50 this.flags = Collections.unmodifiableList(new ArrayList<>(builder.flags));
51 }
52
53 /**
54 * Returns a builder for this object.
55 * @return builder to configure and instantiate a RinchiOption object
56 */
57 public static RinchiOptionsBuilder builder() {
58 return new RinchiOptionsBuilder();
59 }
60
61 public List<RinchiFlag> getFlags() {
62 return flags;
63 }
64
65 @Override
66 public String toString() {
67 StringBuilder sb = new StringBuilder();
68
69 for (RinchiFlag rinchiFlag : flags) {
70 if (sb.length() > 0) {
71 sb.append(' ');
72 }
73 sb.append(IS_WINDOWS ? "/" : "-");
74 sb.append(rinchiFlag.toString());
75 }
76
77 return sb.toString();
78 }
79
80 /**
81 * Builder for RinchiOptions.
82 */
83 public static class RinchiOptionsBuilder {
84 private final EnumSet<RinchiFlag> flags = EnumSet.noneOf(RinchiFlag.class);
85
86 private RinchiOptionsBuilder() {
87
88 }
89
90 /**
91 * Customize the configuration by adding one or more flags.
92 * @param flags flags to be added to the configuration
93 * @return returns itself to allow for chaining method calls
94 */
95 public RinchiOptionsBuilder withFlag(RinchiFlag... flags) {
96 Collections.addAll(this.flags, flags);
97 return this;
98 }
99
100 /**
101 * Returns an instance of {@link RinchiOptions} as configured by this builder.
102 * @return an instance of RinchiOptions whose configuration is customized according to this builder
103 */
104 public RinchiOptions build() {
105 return new RinchiOptions(this);
106 }
107 }
108 }