1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the GNU General Public License v3+, or later.
7 // See license.mkd for licensing and copyright information.
8 // ---------------------------------------------------------------------------
10 package cc
.squirreljme
.plugin
;
12 import java
.io
.PrintStream
;
14 import java
.util
.NoSuchElementException
;
16 import java
.util
.SortedSet
;
17 import java
.util
.TreeMap
;
18 import java
.util
.TreeSet
;
19 import org
.gradle
.api
.Project
;
22 * This is a mapping of error codes to projects.
26 public final class ErrorCodeManager
28 /** The project for discovery. */
29 protected final Project project
;
31 /** The mapping of error codes to projects. */
32 private final Map
<String
, Project
> _codeMap
=
33 new TreeMap
<>(String
.CASE_INSENSITIVE_ORDER
);
35 /** Unclaimed error codes. */
36 private final SortedSet
<String
> _unclaimed
=
37 new TreeSet
<>(String
.CASE_INSENSITIVE_ORDER
);
40 * Initializes the error code manager.
42 * @param __project The root project to manage.
43 * @throws NullPointerException On null arguments.
46 public ErrorCodeManager(Project __project
)
47 throws NullPointerException
49 if (__project
== null)
50 throw new NullPointerException("No project.");
52 this.project
= __project
.getRootProject();
54 // Add all unclaimed codes
55 Set
<String
> unclaimed
= this._unclaimed
;
56 for (char a
= 'A'; a
<= 'Z'; a
++)
57 for (char b
= 'A'; b
<= 'Z'; b
++)
58 unclaimed
.add(a
+ "" + b
);
62 * Returns an error list manager for the given code ID.
64 * @param __code The code to get.
65 * @return The error list manager.
68 public final ErrorListManager
errorList(String __code
)
70 return new ErrorListManager(this.projectByCode(__code
));
74 * Returns the next available error code.
76 * @return The next available error code.
79 public final String
next()
85 return this._unclaimed
.first();
90 * Prints the error codes to the given stream.
92 * @param __out The stream to print to.
93 * @throws NullPointerException On null arguments.
96 public final void print(PrintStream __out
)
97 throws NullPointerException
100 throw new NullPointerException("No output stream.");
104 // Print out every project
105 Map
<String
, Project
> codeMap
= this._codeMap
;
108 for (Map
.Entry
<String
, Project
> e
: codeMap
.entrySet())
109 __out
.printf("%2s: %s%n", e
.getKey(), e
.getValue().getName());
114 * Returns the project which owns the given code.
116 * @param __code The code to obtain.
117 * @throws NoSuchElementException If no such project exists.
118 * @return The resulting
120 public final Project
projectByCode(String __code
)
121 throws NoSuchElementException
126 Map
<String
, Project
> codeMap
= this._codeMap
;
129 Project rv
= codeMap
.get(__code
);
131 throw new IllegalArgumentException(
132 String
.format("No project for code %s", __code
));
139 * Initializes the code map.
143 private void __init()
145 Map
<String
, Project
> codeMap
= this._codeMap
;
146 Set
<String
> unclaimed
= this._unclaimed
;
149 // Already has been loaded?
150 if (!codeMap
.isEmpty())
153 // Add all projects found to the list
154 for (Project sub
: this.project
.getAllprojects())
156 // Only choose SquirrelJME projects
157 SquirrelJMEPluginConfiguration config
= sub
.getExtensions()
158 .<SquirrelJMEPluginConfiguration
>findByType(
159 SquirrelJMEPluginConfiguration
.class);
163 String code
= config
.javaDocErrorCode
;
167 "WARNING: Project %s has no error code.\n",
170 // Cannot do anything at this point
174 Project dup
= codeMap
.put(code
.toUpperCase(), sub
);
177 "WARNING: Project %s shares error code %s with %s.\n",
178 sub
.getName(), code
, dup
.getName());
180 unclaimed
.remove(code
.toUpperCase());