From e05f4f42ef57c907df5cc0fc4b79a7ba37006e3b Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Sat, 15 Aug 2015 00:02:56 +0200 Subject: [PATCH] =?utf8?q?Continued=20with=20jcore:=20-=20Added=20interfac?= =?utf8?q?e=20Logical=20for=20logical=20matches=20...=20-=20Continued=20wi?= =?utf8?q?th=20matches()=20method=20in=20SearchCriteria:=20Now=20only=20si?= =?utf8?q?ngle=20column=20tests=20are=20supported,=20but=20more=20follow!?= =?utf8?q?=20Signed-off-by:Roland=20H=C3=A4der=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../mxchange/jcore/criteria/BaseCriteria.java | 22 ++++++++++ .../jcore/criteria/logical/Logical.java | 38 +++++++++++++++++ .../criteria/searchable/SearchCriteria.java | 41 +++++++++++++++++-- .../searchable/SearchableCritera.java | 7 +++- 4 files changed, 103 insertions(+), 5 deletions(-) create mode 100644 src/org/mxchange/jcore/criteria/logical/Logical.java diff --git a/src/org/mxchange/jcore/criteria/BaseCriteria.java b/src/org/mxchange/jcore/criteria/BaseCriteria.java index bb3550d..fd81c2e 100644 --- a/src/org/mxchange/jcore/criteria/BaseCriteria.java +++ b/src/org/mxchange/jcore/criteria/BaseCriteria.java @@ -20,6 +20,7 @@ import java.util.HashMap; import java.util.Map; import java.util.Set; import org.mxchange.jcore.BaseFrameworkSystem; +import org.mxchange.jcore.criteria.logical.Logical; /** * A general criteria class @@ -32,6 +33,11 @@ public class BaseCriteria extends BaseFrameworkSystem implements Criteria { */ private final Map criteria; + /** + * Logical matcher instance + */ + private Logical logcial; + /** * Protected default construtctor */ @@ -73,4 +79,20 @@ public class BaseCriteria extends BaseFrameworkSystem implements Criteria { return this.criteria.values(); } + + /** + * Getter for Logical matcher instance + * + * @return Logical matcher instance + */ + protected final Logical getLogical () { + return this.logcial; + } + + /** + * @param logcial the logcial to set + */ + public final void setLogcial (final Logical logcial) { + this.logcial = logcial; + } } diff --git a/src/org/mxchange/jcore/criteria/logical/Logical.java b/src/org/mxchange/jcore/criteria/logical/Logical.java new file mode 100644 index 0000000..2f26db4 --- /dev/null +++ b/src/org/mxchange/jcore/criteria/logical/Logical.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2015 Roland Haeder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.jcore.criteria.logical; + +import java.util.Map; +import java.util.Set; +import org.mxchange.jcore.FrameworkInterface; + +/** + * An interface for logical tests + * + * @author Roland Haeder + */ +public interface Logical extends FrameworkInterface { + + /** + * Checks logical match condition + * + * @param entrySet Entry set where the values are + * @param criteraMatches Criteria matches + * @return Whether it matches + */ + public boolean matches (final Set> entrySet, final Map criteraMatches); +} diff --git a/src/org/mxchange/jcore/criteria/searchable/SearchCriteria.java b/src/org/mxchange/jcore/criteria/searchable/SearchCriteria.java index fbd8894..d9831e5 100644 --- a/src/org/mxchange/jcore/criteria/searchable/SearchCriteria.java +++ b/src/org/mxchange/jcore/criteria/searchable/SearchCriteria.java @@ -16,7 +16,10 @@ */ package org.mxchange.jcore.criteria.searchable; +import org.mxchange.jcore.criteria.logical.Logical; +import java.lang.reflect.InvocationTargetException; import java.text.MessageFormat; +import java.util.HashMap; import java.util.Map; import org.mxchange.jcore.criteria.BaseCriteria; import org.mxchange.jcore.database.storage.Storeable; @@ -64,7 +67,7 @@ public class SearchCriteria extends BaseCriteria implements SearchableCritera { } @Override - public boolean matches (final Storeable storeable) { + public boolean matches (final Storeable storeable) throws IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException { // Trace message this.getLogger().trace(MessageFormat.format("storeable={0} - CALLED!", storeable)); @@ -74,13 +77,43 @@ public class SearchCriteria extends BaseCriteria implements SearchableCritera { throw new NullPointerException("storeable is null"); } - // Default is matching - boolean matches = true; + // Init matches array + Map criteraMatches = new HashMap<>(this.entrySet().size()); // Check all conditions for (final Map.Entry criteria : this.entrySet()) { // Debug message - this.getLogger().debug(MessageFormat.format("criteria: key={0},value={1}", criteria.getKey(), criteria.getValue())); + this.getLogger().debug(MessageFormat.format("criteria: key={0},value[{1}]={2}", criteria.getKey(), criteria.getValue().getClass().getSimpleName(), criteria.getValue())); + + // Get value from column name + Object value = storeable.getValueFromColumn(criteria.getKey()); + + // Debug message + this.getLogger().debug("value[" + value.getClass().getSimpleName() + "]=" + value); + + // Is both same? + if (value.equals(criteria.getValue())) { + // Debug message + this.getLogger().debug("value=" + value + " - MATCHES!"); + + // Matching criteria found + criteraMatches.put(criteria.getKey(), true); + } + } + + // Init matches + boolean matches; + + // Is only one entry given? + if (this.entrySet().size() == 1) { + // Okay, one criteria only, no need for a "complex" logical match check + matches = (criteraMatches.size() == 1); + } else if (this.getLogical() == null) { + // Logical instance is null + throw new NullPointerException("logical is not set, but more than one column shall be matched."); + } else { + // Now for the final test + matches = this.getLogical().matches(this.entrySet(), criteraMatches); } // Trace message diff --git a/src/org/mxchange/jcore/criteria/searchable/SearchableCritera.java b/src/org/mxchange/jcore/criteria/searchable/SearchableCritera.java index 8455ba5..145d3c9 100644 --- a/src/org/mxchange/jcore/criteria/searchable/SearchableCritera.java +++ b/src/org/mxchange/jcore/criteria/searchable/SearchableCritera.java @@ -16,6 +16,7 @@ */ package org.mxchange.jcore.criteria.searchable; +import java.lang.reflect.InvocationTargetException; import org.mxchange.jcore.criteria.Criteria; import org.mxchange.jcore.database.storage.Storeable; @@ -30,8 +31,12 @@ public interface SearchableCritera extends Criteria { * * @param storeable A Storeable instance to check * @return Whether the Storeable instance matches + * @throws IllegalArgumentException Some implementations may throw this + * @throws java.lang.NoSuchMethodException If the invoked method was not found + * @throws java.lang.IllegalAccessException If the method cannot be accessed + * @throws java.lang.reflect.InvocationTargetException Any other problems? */ - public boolean matches (final Storeable storeable); + public boolean matches (final Storeable storeable) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException; /** * Setter for limit of possible matches -- 2.39.5