- Added interface Logical for logical matches ...
- Continued with matches() method in SearchCriteria: Now only single column tests are supported, but more follow!
Signed-off-by:Roland Häder <roland@mxchange.org>
import java.util.Map;
import java.util.Set;
import org.mxchange.jcore.BaseFrameworkSystem;
+import org.mxchange.jcore.criteria.logical.Logical;
/**
* A general criteria class
*/
private final Map<String, Object> criteria;
+ /**
+ * Logical matcher instance
+ */
+ private Logical logcial;
+
/**
* Protected default construtctor
*/
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;
+ }
}
--- /dev/null
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+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<Map.Entry<String, Object>> entrySet, final Map<String, Boolean> criteraMatches);
+}
*/
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;
}
@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));
throw new NullPointerException("storeable is null");
}
- // Default is matching
- boolean matches = true;
+ // Init matches array
+ Map<String, Boolean> criteraMatches = new HashMap<>(this.entrySet().size());
// Check all conditions
for (final Map.Entry<String, Object> 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
*/
package org.mxchange.jcore.criteria.searchable;
+import java.lang.reflect.InvocationTargetException;
import org.mxchange.jcore.criteria.Criteria;
import org.mxchange.jcore.database.storage.Storeable;
*
* @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