import java.util.HashMap;
import java.util.Map;
+import java.util.Set;
import org.mxchange.jcore.BaseFrameworkSystem;
/**
// Add to map
this.criteria.put(key, value);
}
+
+ /**
+ * Gets all values from underlaying map in an iterator.
+ *
+ * @return Values iteratable
+ */
+ @Override
+ public Iterable<Object> values () {
+ // Call map's method
+ return this.criteria.values();
+
+ }
+
+ /**
+ * Gets all entries as a key-value pair
+ *
+ * @return Key-value paira of all entries
+ */
+ @Override
+ public Set<Map.Entry<String, Object>> entrySet () {
+ return this.criteria.entrySet();
+ }
}
*/
+import java.util.Map;
+import java.util.Set;
import org.mxchange.jcore.FrameworkInterface;
/**
* @param value Value of criteria
*/
public void addCriteria (final String key, final boolean value);
+
+ /**
+ * Gets all values from underlaying map in an iterator.
+ *
+ * @return Values iteratable
+ */
+ public Iterable<Object> values ();
+
+ /**
+ * Gets all entries as a key-value pair
+ *
+ * @return Key-value paira of all entries
+ */
+ public Set<Map.Entry<String, Object>> entrySet ();
}
*/
package org.mxchange.jcore.criteria.searchable;
+import java.text.MessageFormat;
+import java.util.Map;
import org.mxchange.jcore.criteria.BaseCriteria;
+import org.mxchange.jcore.database.storage.Storeable;
/**
* A search criteria class
*/
public SearchCriteria () {
}
+
+ @Override
+ public boolean matchesAnd (final Storeable storeable) {
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("storeable={0} - CALLED!", storeable));
+
+ // Must not be null
+ if (storeable == null) {
+ // Abort here
+ throw new NullPointerException("storeable is null");
+ }
+
+ // Default is matching
+ boolean matches = true;
+
+ // 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()));
+ }
+
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("matches={0} - EXIT!", matches));
+
+ // Return result
+ return matches;
+ }
}
package org.mxchange.jcore.criteria.searchable;
import org.mxchange.jcore.criteria.Criteria;
+import org.mxchange.jcore.database.storage.Storeable;
/*
* Copyright (C) 2015 Roland Haeder
* @author Roland Haeder
*/
public interface SearchableCritera extends Criteria {
+
+ /**
+ * Checks if the given instance of a Storeable class matchesAnd all criteria
+ *
+ * @param storeable A Storeable instance to check
+ * @return Whether the Storeable instance matchesAnd all criteria
+ */
+ public boolean matchesAnd (final Storeable storeable);
}
import org.mxchange.jcore.criteria.searchable.SearchableCritera;
import org.mxchange.jcore.database.result.Result;
import org.mxchange.jcore.database.storage.Storeable;
+import org.mxchange.jcore.exceptions.BadTokenException;
/**
* A generic interface for database frontends
*
* @param critera Search critera
* @return A result instance
+ * @throws java.io.IOException If any IO error occurs
+ * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found
*/
- public Result<? extends Storeable> doSelectByCriteria (final SearchableCritera critera);
+ public Result<? extends Storeable> doSelectByCriteria (final SearchableCritera critera) throws IOException, BadTokenException;
/**
* Shuts down this backend
*/
package org.mxchange.jcore.database.backend.base64;
+import org.mxchange.jcore.database.result.DatabaseResult;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
-import java.sql.SQLException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
* This database backend does not need to connect
*/
@Override
- public void connectToDatabase () throws SQLException {
+ public void connectToDatabase () {
// Empty body
}
+ /**
+ * Searches for given criteria over a file-based database. This method does
+ * always return a Result instance and never null.
+ *
+ * @param critera SearchableCriteria instance
+ * @return A Result instance for all matching Storeable instances
+ * @throws IOException
+ * @throws BadTokenException
+ */
@Override
- public Result<? extends Storeable> doSelectByCriteria (final SearchableCritera critera) {
- throw new UnsupportedOperationException(MessageFormat.format("Not supported yet: criteria={0}", critera));
+ public Result<? extends Storeable> doSelectByCriteria (final SearchableCritera critera) throws IOException, BadTokenException {
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("criteria={0} - CALLED!", critera));
+
+ // Init result instance
+ Result<Storeable> result = new DatabaseResult();
+
+ // First rewind this backend
+ this.rewind();
+
+ // Then loop over all rows until the end has reached
+ while (this.isEndOfFile()) {
+ // Read line
+ String line = this.readLine();
+
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("line={0}", line));
+
+ // Parse it to a Storeable instance
+ Storeable storeable = this.getFrontend().parseLineToStoreable(line);
+
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("storeable={0}", storeable));
+
+ // Now matchesAnd the found instance
+ if (critera.matchesAnd(storeable)) {
+ // Then add it to result
+ result.add(storeable);
+ }
+ }
+
+ // Return the result
+ return result;
}
/**
* Shuts down this backend
*/
@Override
- public void doShutdown () throws SQLException, IOException {
+ public void doShutdown () throws IOException {
// Trace message
this.getLogger().trace("CALLED!"); //NOI18N
--- /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.database.result;
+
+import java.util.Iterator;
+import org.mxchange.jcore.BaseFrameworkSystem;
+import org.mxchange.jcore.database.storage.Storeable;
+
+/**
+ * A database result
+ * @author Roland Haeder
+ */
+public class DatabaseResult extends BaseFrameworkSystem implements Result<Storeable> {
+ /**
+ * Default constructor
+ */
+ public DatabaseResult () {
+ }
+
+ /**
+ * Given Storeable instance as a query result.
+ *
+ * @param storeable An instance of a Storeable class
+ */
+ @Override
+ public void add (final Storeable storeable) {
+ throw new UnsupportedOperationException("Not supported yet: storeable=" + storeable); //To change body of generated methods, choose Tools | Templates.
+ }
+
+ @Override
+ public boolean hasNext () {
+ throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+ }
+
+ @Override
+ public Iterator<Storeable> iterator () {
+ throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+ }
+
+ @Override
+ public Storeable next () {
+ throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+ }
+
+ @Override
+ public void remove () {
+ throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+ }
+}
* @param <T> Anything that is storeable
*/
public interface Result<T extends Storeable> extends FrameworkInterface, Iterator<T>, Iterable<T> {
+
+ /**
+ * Given Storeable instance as a query result.
+ *
+ * @param storeable An instance of a Storeable class
+ */
+ public void add (final Storeable storeable);
}