Debian Codegraph @

jna-inchi-api/src/main/java/io/github/dan2097/jnainchi/InchiOptions.java

raw
1 /**
2 * JNA-InChI - Library for calling InChI from Java
3 * Copyright © 2018 Daniel Lowe
4 *
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 *
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 *
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.jnainchi;
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 public class InchiOptions {
27
28 static final InchiOptions DEFAULT_OPTIONS = new InchiOptionsBuilder().build();
29
30 private static final boolean IS_WINDOWS = System.getProperty("os.name", "").toLowerCase(Locale.ROOT).startsWith("windows");
31
32 private final List<InchiFlag> flags;
33 private final long timeoutMilliSecs;
34
35 private InchiOptions(InchiOptionsBuilder builder) {
36 this.flags = Collections.unmodifiableList(new ArrayList<InchiFlag>(builder.flags));
37 this.timeoutMilliSecs = builder.timeoutMilliSecs;
38 }
39
40 public static class InchiOptionsBuilder {
41
42 private final EnumSet<InchiFlag> flags = EnumSet.noneOf(InchiFlag.class);
43 private long timeoutMilliSecs = 0;
44
45 public InchiOptionsBuilder withFlag(InchiFlag... flags) {
46 for (InchiFlag flag : flags) {
47 this.flags.add(flag);
48 }
49 return this;
50 }
51
52 /**
53 * Timeout in seconds (0 = infinite timeout)
54 * @param timeoutSecs
55 * @return
56 */
57 public InchiOptionsBuilder withTimeout(int timeoutSecs) {
58 if (timeoutSecs < 0) {
59 throw new IllegalArgumentException("Timeout should be a time in seconds or 0 for infinite: " + timeoutSecs);
60 }
61 this.timeoutMilliSecs = (long) timeoutSecs * 1000;
62 return this;
63 }
64
65 /**
66 * Timeout in milliseconds (0 = infinite timeout)
67 * @param timeoutMilliSecs
68 * @return
69 */
70 public InchiOptionsBuilder withTimeoutMilliSeconds(long timeoutMilliSecs) {
71 if (timeoutMilliSecs < 0) {
72 throw new IllegalArgumentException("Timeout should be a time in milliseconds or 0 for infinite: " + timeoutMilliSecs);
73 }
74 this.timeoutMilliSecs = timeoutMilliSecs;
75 return this;
76 }
77
78 public InchiOptions build() {
79 int stereoOptionFlags = 0;
80 int chiralFlagFlags = 0;
81 for (InchiFlag flag : flags) {
82 switch (flag) {
83 case SNon:
84 case SRac:
85 case SRel:
86 case SUCF:
87 case SAbs:
88 stereoOptionFlags++;
89 break;
90 case ChiralFlagOFF:
91 case ChiralFlagON:
92 chiralFlagFlags++;
93 break;
94 default:
95 break;
96 }
97 }
98 if (stereoOptionFlags > 1) {
99 throw new IllegalArgumentException("Ambiguous flags: SAbs, SNon, SRel, SRac and SUCF are mutually exclusive");
100 }
101 if (chiralFlagFlags > 1) {
102 throw new IllegalArgumentException("Ambiguous flags: ChiralFlagOFF and ChiralFlagON are mutually exclusive");
103 }
104 return new InchiOptions(this);
105 }
106 }
107
108 public List<InchiFlag> getFlags() {
109 return flags;
110 }
111
112 public int getTimeout() {
113 return (int) (timeoutMilliSecs/1000);
114 }
115
116 public long getTimeoutMilliSeconds() {
117 return timeoutMilliSecs;
118 }
119
120 @Override
121 public String toString() {
122 StringBuilder sb = new StringBuilder();
123 for (InchiFlag inchiFlag : flags) {
124 if (inchiFlag == InchiFlag.SAbs) {
125 continue;
126 }
127 if (sb.length() > 0) {
128 sb.append(' ');
129 }
130 sb.append(IS_WINDOWS ? "/" : "-");
131 sb.append(inchiFlag.toString());
132 }
133 if (timeoutMilliSecs != 0) {
134 if (sb.length() > 0) {
135 sb.append(' ');
136 }
137 sb.append(IS_WINDOWS ? "/" : "-");
138 sb.append("WM");
139 sb.append(String.valueOf(timeoutMilliSecs));
140 }
141 return sb.toString();
142 }
143 }