From c3831bd63fe851bd54d27e63078669b1f42487ff Mon Sep 17 00:00:00 2001 From: nathan Date: Sun, 13 Mar 2016 17:37:06 +0100 Subject: [PATCH] Added ToolBox --- Dorkbox-Util/src/dorkbox/util/Tool.java | 21 +++++++ Dorkbox-Util/src/dorkbox/util/ToolBox.java | 68 ++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 Dorkbox-Util/src/dorkbox/util/Tool.java create mode 100644 Dorkbox-Util/src/dorkbox/util/ToolBox.java diff --git a/Dorkbox-Util/src/dorkbox/util/Tool.java b/Dorkbox-Util/src/dorkbox/util/Tool.java new file mode 100644 index 0000000..f7349b7 --- /dev/null +++ b/Dorkbox-Util/src/dorkbox/util/Tool.java @@ -0,0 +1,21 @@ +/* + * Copyright 2010 dorkbox, llc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package dorkbox.util; + +public +interface Tool { + +} diff --git a/Dorkbox-Util/src/dorkbox/util/ToolBox.java b/Dorkbox-Util/src/dorkbox/util/ToolBox.java new file mode 100644 index 0000000..176e591 --- /dev/null +++ b/Dorkbox-Util/src/dorkbox/util/ToolBox.java @@ -0,0 +1,68 @@ +/* + * Copyright 2010 dorkbox, llc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package dorkbox.util; + +import java.util.concurrent.ConcurrentHashMap; + +/** + * Allows for different objects to be reused in the system directly + */ +public +class ToolBox { + + private final ConcurrentHashMap, dorkbox.util.Tool> toolMap = new ConcurrentHashMap, dorkbox.util.Tool>(); + + /** + * Registers a tool with the server, to be used by other services. + */ + public + void register(Tool toolClass) { + if (toolClass == null) { + throw new IllegalArgumentException("Tool must not be null! Unable to add tool"); + } + + dorkbox.util.Tool put = this.toolMap.put(toolClass.getClass(), toolClass); + if (put != null) { + throw new IllegalArgumentException("Tool must be unique! Unable to add tool '" + toolClass + "'"); + } + } + + /** + * Only get the tools in the ModuleStart (ie: load) methods. If used in the constructor, the tool might not be available yet + */ + public + Tool get(Class toolClass) { + if (toolClass == null) { + throw new IllegalArgumentException("Tool must not be null! Unable to add tool"); + } + + @SuppressWarnings("unchecked") + Tool tool = (Tool) this.toolMap.get(toolClass); + return tool; + } + + /** + * Only get the tools in the ModuleStart (ie: load) methods. If done in the constructor, the tool might not be available yet + */ + public + void remove(Class toolClass) { + if (toolClass == null) { + throw new IllegalArgumentException("Tool must not be null! Unable to remove tool"); + } + + this.toolMap.remove(toolClass); + } +}