Remove clashing error prefix; Use better name for RatufaCoat ROMs.
[SquirrelJME.git] / buildSrc / src / main / java / cc / squirreljme / plugin / ErrorCodeManager.java
blob36c08c668cf6fca70e14598348bc017cefe9ebf0
1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // SquirrelJME
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;
13 import java.util.Map;
14 import java.util.NoSuchElementException;
15 import java.util.Set;
16 import java.util.SortedSet;
17 import java.util.TreeMap;
18 import java.util.TreeSet;
19 import org.gradle.api.Project;
21 /**
22 * This is a mapping of error codes to projects.
24 * @since 2020/02/22
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);
39 /**
40 * Initializes the error code manager.
42 * @param __project The root project to manage.
43 * @throws NullPointerException On null arguments.
44 * @since 2020/02/22
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);
61 /**
62 * Returns an error list manager for the given code ID.
64 * @param __code The code to get.
65 * @return The error list manager.
66 * @since 2020/02/22
68 public final ErrorListManager errorList(String __code)
70 return new ErrorListManager(this.projectByCode(__code));
73 /**
74 * Returns the next available error code.
76 * @return The next available error code.
77 * @since 2020/02/22
79 public final String next()
81 this.__init();
83 synchronized (this)
85 return this._unclaimed.first();
89 /**
90 * Prints the error codes to the given stream.
92 * @param __out The stream to print to.
93 * @throws NullPointerException On null arguments.
94 * @since 2020/02/22
96 public final void print(PrintStream __out)
97 throws NullPointerException
99 if (__out == null)
100 throw new NullPointerException("No output stream.");
102 this.__init();
104 // Print out every project
105 Map<String, Project> codeMap = this._codeMap;
106 synchronized (this)
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
123 this.__init();
125 // Search
126 Map<String, Project> codeMap = this._codeMap;
127 synchronized (this)
129 Project rv = codeMap.get(__code);
130 if (rv == null)
131 throw new IllegalArgumentException(
132 String.format("No project for code %s", __code));
134 return rv;
139 * Initializes the code map.
141 * @since 2020/02/22
143 private void __init()
145 Map<String, Project> codeMap = this._codeMap;
146 Set<String> unclaimed = this._unclaimed;
147 synchronized (this)
149 // Already has been loaded?
150 if (!codeMap.isEmpty())
151 return;
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);
160 if (config == null)
161 continue;
163 String code = config.javaDocErrorCode;
164 if (code == null)
166 System.err.printf(
167 "WARNING: Project %s has no error code.\n",
168 sub.getName());
170 // Cannot do anything at this point
171 continue;
174 Project dup = codeMap.put(code.toUpperCase(), sub);
175 if (dup != null)
176 System.err.printf(
177 "WARNING: Project %s shares error code %s with %s.\n",
178 sub.getName(), code, dup.getName());
180 unclaimed.remove(code.toUpperCase());