Begun work on extracting data providers

This commit is contained in:
Rsl1122 2019-03-13 12:59:33 +02:00
parent a52954dbb8
commit 305d0fd77c
6 changed files with 350 additions and 0 deletions

View File

@ -1,5 +1,6 @@
dependencies {
compile "com.djrapitops:AbstractPluginFramework-api:$abstractPluginFrameworkVersion"
compile project(path: ":api", configuration: 'shadow')
compile "com.djrapitops:PlanPluginBridge:$planPluginBridgeVersion"
compile "org.apache.httpcomponents:httpclient:$httpClientVersion"
compile "org.apache.commons:commons-text:$commonsTextVersion"

View File

@ -0,0 +1,88 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.extension.implementation;
import com.djrapitops.plan.extension.DataExtension;
import com.djrapitops.plan.extension.annotation.PluginInfo;
import com.djrapitops.plan.extension.annotation.Tab;
import com.djrapitops.plan.extension.annotation.TabInfo;
import com.djrapitops.plan.extension.annotation.TabOrder;
import com.djrapitops.plan.extension.extractor.ExtensionExtractor;
import com.djrapitops.plan.extension.icon.Color;
import com.djrapitops.plan.extension.icon.Icon;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* Extracts objects that can be used to obtain data to store in the database.
* <p>
* Goal of this class is to abstract away DataExtension API annotations so that they will not be needed outside when calling methods.
*
* @author Rsl1122
*/
public class DataProviderExtractor {
private final DataExtension extension;
private ExtensionExtractor extensionExtractor;
/**
* Create a DataProviderExtractor.
*
* @param extension DataExtension to extract information from.
* @throws IllegalArgumentException If something is badly wrong with the specified extension class annotations.
*/
public DataProviderExtractor(DataExtension extension) {
this.extension = extension;
extensionExtractor = new ExtensionExtractor(extension);
extensionExtractor.extractAnnotationInformation();
}
public String getPluginName() {
return extensionExtractor.getPluginInfo().name();
}
public Icon getPluginIcon() {
PluginInfo pluginInfo = extensionExtractor.getPluginInfo();
return new Icon(pluginInfo.iconFamily(), pluginInfo.iconName(), pluginInfo.color());
}
public Collection<PluginTab> getPluginTabs() {
Map<String, TabInfo> tabInformation = extensionExtractor.getTabInformation()
.stream().collect(Collectors.toMap(TabInfo::tab, Function.identity(), (one, two) -> one));
return extensionExtractor.getMethodAnnotations().getAnnotations(Tab.class).stream()
.map(Tab::value)
.distinct()
.map(tabName -> {
Optional<TabInfo> tabInfo = Optional.ofNullable(tabInformation.get(tabName));
return new PluginTab(
tabName,
tabInfo.map(info -> new Icon(info.iconFamily(), info.iconName(), Color.NONE)).orElse(null),
tabInfo.map(TabInfo::elementOrder).orElse(null)
);
}).collect(Collectors.toList());
}
public Optional<String[]> getTabOrder() {
return extensionExtractor.getTabOrder().map(TabOrder::value);
}
}

View File

@ -0,0 +1,72 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.extension.implementation;
import com.djrapitops.plan.extension.ElementOrder;
import com.djrapitops.plan.extension.icon.Icon;
import com.djrapitops.plugin.utilities.ArrayUtil;
import java.util.Arrays;
import java.util.Optional;
/**
* Represents a tab of {@link com.djrapitops.plan.extension.DataExtension} defined by {@link com.djrapitops.plan.extension.annotation.Tab} and
* {@link com.djrapitops.plan.extension.annotation.TabInfo} annotations.
*
* @author Rsl1122
*/
public class PluginTab {
private final String tabName;
private final Icon icon; // can be null
private ElementOrder[] elementOrder; // can be null / miss values
public PluginTab(String tabName, Icon icon, ElementOrder[] elementOrder) {
this.tabName = tabName;
this.icon = icon;
this.elementOrder = elementOrder;
}
public String getTabName() {
return tabName;
}
public Optional<Icon> getTabIcon() {
return Optional.ofNullable(icon);
}
public Optional<ElementOrder[]> getTabElementOrder() {
if (elementOrder == null) {
return Optional.empty();
}
ElementOrder[] possibleValues = ElementOrder.values();
if (elementOrder.length < possibleValues.length) {
addMissingElements(possibleValues);
}
return Optional.of(elementOrder);
}
private void addMissingElements(ElementOrder[] possibleValues) {
for (ElementOrder possibleValue : possibleValues) {
if (Arrays.binarySearch(elementOrder, possibleValue) < 0) {
elementOrder = ArrayUtil.merge(elementOrder, new ElementOrder[]{possibleValue});
}
}
}
}

View File

@ -0,0 +1,92 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.extension.implementation.providers;
import com.djrapitops.plan.extension.icon.Icon;
import java.util.Optional;
/**
* Abstract representation of all values a Provider annotation provides.
*
* @author Rsl1122
*/
public abstract class DataProvider<T, K> {
private final String plugin;
private final String condition; // can be null
private final String tab; // can be null
private final int priority;
private final Icon icon;
private final String text;
private final String description;
private final MethodWrapper<T, K> method;
public DataProvider(
String plugin,
String condition,
String tab,
int priority,
Icon icon,
String text,
String description,
MethodWrapper<T, K> method
) {
this.plugin = plugin;
this.condition = condition;
this.tab = tab;
this.priority = priority;
this.icon = icon;
this.text = text;
this.description = description;
this.method = method;
}
public String getPlugin() {
return plugin;
}
public int getPriority() {
return priority;
}
public Icon getIcon() {
return icon;
}
public String getText() {
return text;
}
public String getDescription() {
return description;
}
public Optional<String> getCondition() {
return Optional.ofNullable(condition);
}
public Optional<String> getTab() {
return Optional.ofNullable(tab);
}
public MethodWrapper<T, K> getMethod() {
return method;
}
}

View File

@ -0,0 +1,51 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.extension.implementation.providers;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Group class for handling multiple different types of {@link DataProvider}s.
*
* @author Rsl1122
*/
public class DataProviders {
// Return type, Parameter type, DataProvider
private Map<Class, Map<Class, List<DataProvider>>> providers;
public DataProviders() {
providers = new HashMap<>();
}
public <T, K> void put(Class<T> parameterType, Class<K> returnType, DataProvider<T, K> provider) {
Map<Class, List<DataProvider>> byParameterType = providers.getOrDefault(returnType, new HashMap<>());
List<DataProvider> dataProviders = byParameterType.getOrDefault(parameterType, new ArrayList<>());
dataProviders.add(provider);
byParameterType.put(parameterType, dataProviders);
providers.put(returnType, byParameterType);
}
public <T, K> List<DataProvider> get(Class<T> parameterType, Class<K> returnType) {
return this.providers.getOrDefault(returnType, new HashMap<>()).getOrDefault(parameterType, new ArrayList<>());
}
}

View File

@ -0,0 +1,46 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.extension.implementation.providers;
import com.djrapitops.plan.extension.DataExtension;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* Wrap a Method so that it is easier to call.
*
* @author Rsl1122
*/
public class MethodWrapper<T, K> {
private final Method method;
private final Class<K> result;
public MethodWrapper(Method method, Class<T> parameter, Class<K> result) {
this.method = method;
this.result = result;
}
public K callMethod(DataExtension extension, T parameter) throws InvocationTargetException, IllegalAccessException {
return result.cast(method.invoke(extension, parameter));
}
public String getMethodName() {
return method.getName();
}
}