rzo1 commented on code in PR #775: URL: https://github.com/apache/opennlp/pull/775#discussion_r2066593502
########## opennlp-tools-models/src/main/java/opennlp/tools/models/ClassPathModelLoader.java: ########## @@ -59,4 +66,75 @@ public ClassPathModel load(ClassPathModelEntry entry) throws IOException { return new ClassPathModel(properties, model); } + + /** + * Restores a {@link T model} among a set {@link ClassPathModelEntry classpath entries} + * according to the specified parameters {@code lang} and {@code type}. + * + * @param classPathEntries A non-empty set of {@link ClassPathModelEntry candidates} to find a matching + * model in. Must not be {@code null}. If it is empty, the result will + * be {@code null}. + * @param lang The language code of the requested model. If {@code null}, the result will + * be {@code null} as well. + * @param type The {@link ModelType} to select the model variant. + * @param modelType The class of model type parameter {@link T} to create an instance of. + * + * @return An model instance of type {@link T}, or {@code null} if no match was found + * for the specified parameters. + * + * @throws IllegalArgumentException Thrown if parameters were invalid. + * @throws ClassPathLoaderException Thrown if {@link T} could not be instantiated correctly. + * @throws IOException Thrown if something went wrong during reading resources from the classpath. + */ + <T extends BaseModel> T load(Set<ClassPathModelEntry> classPathEntries, String lang, + ModelType type, Class<T> modelType) throws IOException { + return load(classPathEntries, lang, type.getName(), modelType); Review Comment: Possible NPE. `type` needs a check because `getName()` would be invoked on NULL. ########## opennlp-tools-models/src/main/java/opennlp/tools/models/ClassPathModelLoader.java: ########## @@ -59,4 +66,75 @@ public ClassPathModel load(ClassPathModelEntry entry) throws IOException { return new ClassPathModel(properties, model); } + + /** + * Restores a {@link T model} among a set {@link ClassPathModelEntry classpath entries} + * according to the specified parameters {@code lang} and {@code type}. + * + * @param classPathEntries A non-empty set of {@link ClassPathModelEntry candidates} to find a matching + * model in. Must not be {@code null}. If it is empty, the result will + * be {@code null}. + * @param lang The language code of the requested model. If {@code null}, the result will + * be {@code null} as well. + * @param type The {@link ModelType} to select the model variant. + * @param modelType The class of model type parameter {@link T} to create an instance of. + * + * @return An model instance of type {@link T}, or {@code null} if no match was found + * for the specified parameters. + * + * @throws IllegalArgumentException Thrown if parameters were invalid. + * @throws ClassPathLoaderException Thrown if {@link T} could not be instantiated correctly. + * @throws IOException Thrown if something went wrong during reading resources from the classpath. + */ + <T extends BaseModel> T load(Set<ClassPathModelEntry> classPathEntries, String lang, + ModelType type, Class<T> modelType) throws IOException { + return load(classPathEntries, lang, type.getName(), modelType); + } + + /** + * Restores a {@link T model} among a set {@link ClassPathModelEntry classpath entries} + * according to the specified parameters {@code lang} and {@code type}. + * + * @param classPathEntries A non-empty set of {@link ClassPathModelEntry candidates} to find a matching + * model in. Must not be {@code null}. If it is empty, the result will + * be {@code null}. + * @param lang The language code of the requested model. If {@code null}, the result will + * be {@code null} as well. + * @param type The type string to narrow down the model variant. Note: Custom naming patterns + * can be applied here, as the {@code type} fragment will be used for a 'contains' + * check internally. + * @param modelType The class of model type parameter {@link T} to create an instance of. + * + * @return An model instance of type {@link T}, or {@code null} if no match was found + * for the specified parameters. + * + * @throws IllegalArgumentException Thrown if parameters were invalid. + * @throws ClassPathLoaderException Thrown if {@link T} could not be instantiated correctly. + * @throws IOException Thrown if something went wrong during reading resources from the classpath. + */ + <T extends BaseModel> T load(Set<ClassPathModelEntry> classPathEntries, String lang, + String type, Class<T> modelType) throws IOException { + if (classPathEntries == null) { + throw new IllegalArgumentException("The provided ClassPath entries must not be null!"); + } + T result = null; + if (classPathEntries.isEmpty()) { + return result; + } + try { + for (ClassPathModelEntry entry : classPathEntries) { + final ClassPathModel cpm = load(entry); + if (cpm != null && cpm.getModelLanguage().equals(lang) && cpm.getModelName().contains(type)) { + try (InputStream is = new BufferedInputStream(new ByteArrayInputStream(cpm.model()))) { + result = modelType.getConstructor(InputStream.class).newInstance(is); Review Comment: `modelType` needs a check for != null (also in Javadoc) ########## opennlp-tools-models/src/main/java/opennlp/tools/models/ClassPathModelProvider.java: ########## @@ -0,0 +1,77 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 opennlp.tools.models; + +import java.io.IOException; + +import opennlp.tools.util.model.BaseModel; + +/** + * A provider for obtaining pre-trained OpenNLP {@link BaseModel models} from + * an application's classpath. + * <p> + * Providing models available in the classpath requires a {@link ClassPathModelFinder finder} + * and a {@link ClassPathModelLoader} at runtime. Depending on the environment, + * a custom {@code finder} can be used. + * + * @see BaseModel + * @see ClassPathModelFinder + * @see ClassPathModelLoader + */ +public interface ClassPathModelProvider { + + /** + * Restores a {@link T model} among all classpath models at runtime + * according to the specified parameters {@code lang} and {@code type}. + * + * @param lang The ISO language code of the requested model. If {@code null}, the result will + * be {@code null} as well. + * @param type The type string to narrow down the model variant. Note: Custom naming patterns + * can be applied here, as the {@code type} fragment will be used for a 'contains' + * check internally. + * @param modelType The class of model type parameter {@link T} to create an instance of. Review Comment: `It must not be {@code null}.` ########## opennlp-tools-models/src/main/java/opennlp/tools/models/ClassPathModelProvider.java: ########## @@ -0,0 +1,77 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 opennlp.tools.models; + +import java.io.IOException; + +import opennlp.tools.util.model.BaseModel; + +/** + * A provider for obtaining pre-trained OpenNLP {@link BaseModel models} from + * an application's classpath. + * <p> + * Providing models available in the classpath requires a {@link ClassPathModelFinder finder} + * and a {@link ClassPathModelLoader} at runtime. Depending on the environment, + * a custom {@code finder} can be used. + * + * @see BaseModel + * @see ClassPathModelFinder + * @see ClassPathModelLoader + */ +public interface ClassPathModelProvider { + + /** + * Restores a {@link T model} among all classpath models at runtime + * according to the specified parameters {@code lang} and {@code type}. + * + * @param lang The ISO language code of the requested model. If {@code null}, the result will + * be {@code null} as well. + * @param type The type string to narrow down the model variant. Note: Custom naming patterns + * can be applied here, as the {@code type} fragment will be used for a 'contains' + * check internally. + * @param modelType The class of model type parameter {@link T} to create an instance of. + * + * @return An model instance of type {@link T}, or {@code null} if no match was found + * for the specified parameters. + * @throws IllegalArgumentException Thrown if parameters were invalid. + * @throws IOException Thrown if something went wrong during reading resources from the classpath. + */ + <T extends BaseModel> T load(String lang, ModelType type, Class<T> modelType) + throws IOException; + + /** + * Restores a {@link T model} among all classpath models at runtime + * according to the specified parameters {@code lang} and {@code type}. + * + * @param lang The ISO language code of the requested model. If {@code null}, the result will Review Comment: What happens if emptry string? ########## opennlp-tools-models/src/main/java/opennlp/tools/models/ClassPathModelProvider.java: ########## @@ -0,0 +1,77 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 opennlp.tools.models; + +import java.io.IOException; + +import opennlp.tools.util.model.BaseModel; + +/** + * A provider for obtaining pre-trained OpenNLP {@link BaseModel models} from + * an application's classpath. + * <p> + * Providing models available in the classpath requires a {@link ClassPathModelFinder finder} + * and a {@link ClassPathModelLoader} at runtime. Depending on the environment, + * a custom {@code finder} can be used. + * + * @see BaseModel + * @see ClassPathModelFinder + * @see ClassPathModelLoader + */ +public interface ClassPathModelProvider { + + /** + * Restores a {@link T model} among all classpath models at runtime + * according to the specified parameters {@code lang} and {@code type}. + * + * @param lang The ISO language code of the requested model. If {@code null}, the result will Review Comment: What happens if empty string? ########## opennlp-tools-models/src/main/java/opennlp/tools/models/ClassPathModelProvider.java: ########## @@ -0,0 +1,77 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 opennlp.tools.models; + +import java.io.IOException; + +import opennlp.tools.util.model.BaseModel; + +/** + * A provider for obtaining pre-trained OpenNLP {@link BaseModel models} from + * an application's classpath. + * <p> + * Providing models available in the classpath requires a {@link ClassPathModelFinder finder} + * and a {@link ClassPathModelLoader} at runtime. Depending on the environment, + * a custom {@code finder} can be used. + * + * @see BaseModel + * @see ClassPathModelFinder + * @see ClassPathModelLoader + */ +public interface ClassPathModelProvider { + + /** + * Restores a {@link T model} among all classpath models at runtime + * according to the specified parameters {@code lang} and {@code type}. + * + * @param lang The ISO language code of the requested model. If {@code null}, the result will + * be {@code null} as well. + * @param type The type string to narrow down the model variant. Note: Custom naming patterns + * can be applied here, as the {@code type} fragment will be used for a 'contains' + * check internally. + * @param modelType The class of model type parameter {@link T} to create an instance of. + * + * @return An model instance of type {@link T}, or {@code null} if no match was found + * for the specified parameters. + * @throws IllegalArgumentException Thrown if parameters were invalid. + * @throws IOException Thrown if something went wrong during reading resources from the classpath. + */ + <T extends BaseModel> T load(String lang, ModelType type, Class<T> modelType) + throws IOException; + + /** + * Restores a {@link T model} among all classpath models at runtime + * according to the specified parameters {@code lang} and {@code type}. + * + * @param lang The ISO language code of the requested model. If {@code null}, the result will + * be {@code null} as well. + * @param type The type string to narrow down the model variant. Note: Custom naming patterns + * can be applied here, as the {@code type} fragment will be used for a 'contains' + * check internally. + * @param modelType The class of model type parameter {@link T} to create an instance of. Review Comment: `It must not be {@code null}.` ########## opennlp-tools-models/src/main/java/opennlp/tools/models/ClassPathModelProvider.java: ########## @@ -0,0 +1,77 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 opennlp.tools.models; + +import java.io.IOException; + +import opennlp.tools.util.model.BaseModel; + +/** + * A provider for obtaining pre-trained OpenNLP {@link BaseModel models} from + * an application's classpath. + * <p> + * Providing models available in the classpath requires a {@link ClassPathModelFinder finder} + * and a {@link ClassPathModelLoader} at runtime. Depending on the environment, + * a custom {@code finder} can be used. + * + * @see BaseModel + * @see ClassPathModelFinder + * @see ClassPathModelLoader + */ +public interface ClassPathModelProvider { + + /** + * Restores a {@link T model} among all classpath models at runtime + * according to the specified parameters {@code lang} and {@code type}. + * + * @param lang The ISO language code of the requested model. If {@code null}, the result will + * be {@code null} as well. + * @param type The type string to narrow down the model variant. Note: Custom naming patterns + * can be applied here, as the {@code type} fragment will be used for a 'contains' + * check internally. + * @param modelType The class of model type parameter {@link T} to create an instance of. + * + * @return An model instance of type {@link T}, or {@code null} if no match was found + * for the specified parameters. + * @throws IllegalArgumentException Thrown if parameters were invalid. + * @throws IOException Thrown if something went wrong during reading resources from the classpath. + */ + <T extends BaseModel> T load(String lang, ModelType type, Class<T> modelType) + throws IOException; + + /** + * Restores a {@link T model} among all classpath models at runtime + * according to the specified parameters {@code lang} and {@code type}. + * + * @param lang The ISO language code of the requested model. If {@code null}, the result will + * be {@code null} as well. + * @param type The type string to narrow down the model variant. Note: Custom naming patterns Review Comment: `type` is not a string here. Most likely a ref towards `ModelType` ? ########## opennlp-tools-models/src/main/java/opennlp/tools/models/ClassPathModelProvider.java: ########## @@ -0,0 +1,77 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 opennlp.tools.models; + +import java.io.IOException; + +import opennlp.tools.util.model.BaseModel; + +/** + * A provider for obtaining pre-trained OpenNLP {@link BaseModel models} from + * an application's classpath. + * <p> + * Providing models available in the classpath requires a {@link ClassPathModelFinder finder} + * and a {@link ClassPathModelLoader} at runtime. Depending on the environment, + * a custom {@code finder} can be used. + * + * @see BaseModel + * @see ClassPathModelFinder + * @see ClassPathModelLoader + */ +public interface ClassPathModelProvider { + + /** + * Restores a {@link T model} among all classpath models at runtime + * according to the specified parameters {@code lang} and {@code type}. + * + * @param lang The ISO language code of the requested model. If {@code null}, the result will + * be {@code null} as well. + * @param type The type string to narrow down the model variant. Note: Custom naming patterns Review Comment: type is not a string here. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: dev-unsubscr...@opennlp.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org