// Path: core/src/main/java/org/n52/supervisor/db/ResultDatabase.java
// @Singleton
// public class ResultDatabase {
// 
//     public static final CheckResult NO_CHECK_RESULT_WITH_GIVEN_ID = new CheckResult() {};
// 
// 	private Queue<CheckResult> latestResults;
// 
//     private static Logger log = LoggerFactory.getLogger(ResultDatabase.class);
// 
//     private final int maxStoredResults;
// 
//     @Inject
//     public ResultDatabase(@Named("supervisor.checks.maxStoredResults") final
//     int maxStoredResults) {
//         this.maxStoredResults = maxStoredResults;
//         latestResults = EvictingQueue.create(this.maxStoredResults);
//         log.info("NEW {}", this);
//     }
// 
//     public void appendResult(final CheckResult result) {
//     	if (!latestResults.contains(result)) {
//     		latestResults.add(result);
//     	}
//     }
// 
//     public void appendResults(final Collection<CheckResult> results) {
//         // if (latestResults.size() >= 100) { // FIXME make append non static so that config parameter can be
//         // // used: this.maxStoredResults
//         // log.debug("Too many results. Got " + results.size() + " new and " + latestResults.size() +
//         // " existing.");
//         // for (int i = 0; i < Math.min(results.size(), latestResults.size()); i++) {
//         // // remove the first element so many times that the new results
//         // // fit.
//         // latestResults.remove();
//         // }
//         // }
// 
//         latestResults.addAll(results);
//     }
// 
//     public void clearResults() {
//         log.debug("Clearing all results: {}", Arrays.deepToString(latestResults.toArray()));
//         latestResults.clear();
//     }
// 
//     public void close() {
//         latestResults.clear();
//         latestResults = null;
//     }
// 
//     public List<CheckResult> getLatestResults() {
//         return new ArrayList<CheckResult>(latestResults);
//     }
// 
//     public int getMaxStoredResults() {
//         return maxStoredResults;
//     }
// 
//     /**
//      * @param id the identifier of a {@link CheckResult}.
//      * @return The {@link CheckResult} with the given id or {@link ResultDatabase#NO_CHECK_RESULT_WITH_GIVEN_ID}.
//      */
//     public CheckResult getResult(final String id){
//         for (final CheckResult checkResult : latestResults) {
// 			if (checkResult.getIdentifier().equals(id)) {
// 				return checkResult;
// 			}
// 		}
//         return NO_CHECK_RESULT_WITH_GIVEN_ID;
//     }
// 
//     @Override
//     public String toString() {
//         final StringBuilder builder = new StringBuilder();
//         builder.append("ResultDatabase [maxStoredResults=");
//         builder.append(maxStoredResults);
//         builder.append("]");
//         return builder.toString();
//     }
// 
// 	public boolean isEmpty() {
// 		return latestResults.isEmpty();
// 	}
// 
// }
// 
// Path: core/src/main/java/org/n52/supervisor/id/IdentifierGenerator.java
// public interface IdentifierGenerator {
// 
//     public abstract String generate();
// 
//     public abstract Collection<String> generate(int count);
// 
// }
// 
// Path: core/src/main/java/org/n52/supervisor/id/ShortAlphanumericIdentifierGenerator.java
// public class ShortAlphanumericIdentifierGenerator implements IdentifierGenerator {
// 
//     private static Logger log = LoggerFactory.getLogger(ShortAlphanumericIdentifierGenerator.class);
// 
//     public static final int ID_LENGTH = 10;
// 
//     public ShortAlphanumericIdentifierGenerator() {
//         log.debug("NEW {}", this);
//     }
// 
//     @Override
//     public String generate() {
//         return RandomStringUtils.randomAlphanumeric(ID_LENGTH).toLowerCase();
//     }
// 
//     @Override
//     public Collection<String> generate(int count) {
//         ArrayList<String> ids = new ArrayList<>();
//         for (int i = 0; i < count; i++) {
//             ids.add(generate());
//         }
//         return ids;
//     }
// 
// }


import java.util.Collection;
import org.n52.supervisor.db.ResultDatabase;
import org.n52.supervisor.id.IdentifierGenerator;
import org.n52.supervisor.id.ShortAlphanumericIdentifierGenerator;
/**
 * ﻿Copyright (C) 2013 - 2014 52°North Initiative for Geospatial Open Source Software GmbH
 *
 * Licensed 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 org.n52.supervisor.api;



/**
 * @author Daniel Nüst
 *
 */
public interface CheckRunner {

	final IdentifierGenerator ID_GENERATOR = new ShortAlphanumericIdentifierGenerator();

    public void addResult(CheckResult r);

    public boolean check();

    public Check getCheck();

    public Collection<CheckResult> getResults();
    
    public Collection<CheckResult> getAndClearResults();

    public void notifyFailure();

    public void notifySuccess();

    public void setCheck(Check check) throws UnsupportedCheckException;

    /**
     * @param rd
     *        is required so that the runner can announce its results
     */
// Path: core/src/main/java/org/n52/supervisor/db/ResultDatabase.java
// @Singleton
// public class ResultDatabase {
// 
//     public static final CheckResult NO_CHECK_RESULT_WITH_GIVEN_ID = new CheckResult() {};
// 
// 	private Queue<CheckResult> latestResults;
// 
//     private static Logger log = LoggerFactory.getLogger(ResultDatabase.class);
// 
//     private final int maxStoredResults;
// 
//     @Inject
//     public ResultDatabase(@Named("supervisor.checks.maxStoredResults") final
//     int maxStoredResults) {
//         this.maxStoredResults = maxStoredResults;
//         latestResults = EvictingQueue.create(this.maxStoredResults);
//         log.info("NEW {}", this);
//     }
// 
//     public void appendResult(final CheckResult result) {
//     	if (!latestResults.contains(result)) {
//     		latestResults.add(result);
//     	}
//     }
// 
//     public void appendResults(final Collection<CheckResult> results) {
//         // if (latestResults.size() >= 100) { // FIXME make append non static so that config parameter can be
//         // // used: this.maxStoredResults
//         // log.debug("Too many results. Got " + results.size() + " new and " + latestResults.size() +
//         // " existing.");
//         // for (int i = 0; i < Math.min(results.size(), latestResults.size()); i++) {
//         // // remove the first element so many times that the new results
//         // // fit.
//         // latestResults.remove();
//         // }
//         // }
// 
//         latestResults.addAll(results);
//     }
// 
//     public void clearResults() {
//         log.debug("Clearing all results: {}", Arrays.deepToString(latestResults.toArray()));
//         latestResults.clear();
//     }
// 
//     public void close() {
//         latestResults.clear();
//         latestResults = null;
//     }
// 
//     public List<CheckResult> getLatestResults() {
//         return new ArrayList<CheckResult>(latestResults);
//     }
// 
//     public int getMaxStoredResults() {
//         return maxStoredResults;
//     }
// 
//     /**
//      * @param id the identifier of a {@link CheckResult}.
//      * @return The {@link CheckResult} with the given id or {@link ResultDatabase#NO_CHECK_RESULT_WITH_GIVEN_ID}.
//      */
//     public CheckResult getResult(final String id){
//         for (final CheckResult checkResult : latestResults) {
// 			if (checkResult.getIdentifier().equals(id)) {
// 				return checkResult;
// 			}
// 		}
//         return NO_CHECK_RESULT_WITH_GIVEN_ID;
//     }
// 
//     @Override
//     public String toString() {
//         final StringBuilder builder = new StringBuilder();
//         builder.append("ResultDatabase [maxStoredResults=");
//         builder.append(maxStoredResults);
//         builder.append("]");
//         return builder.toString();
//     }
// 
// 	public boolean isEmpty() {
// 		return latestResults.isEmpty();
// 	}
// 
// }
// 
// Path: core/src/main/java/org/n52/supervisor/id/IdentifierGenerator.java
// public interface IdentifierGenerator {
// 
//     public abstract String generate();
// 
//     public abstract Collection<String> generate(int count);
// 
// }
// 
// Path: core/src/main/java/org/n52/supervisor/id/ShortAlphanumericIdentifierGenerator.java
// public class ShortAlphanumericIdentifierGenerator implements IdentifierGenerator {
// 
//     private static Logger log = LoggerFactory.getLogger(ShortAlphanumericIdentifierGenerator.class);
// 
//     public static final int ID_LENGTH = 10;
// 
//     public ShortAlphanumericIdentifierGenerator() {
//         log.debug("NEW {}", this);
//     }
// 
//     @Override
//     public String generate() {
//         return RandomStringUtils.randomAlphanumeric(ID_LENGTH).toLowerCase();
//     }
// 
//     @Override
//     public Collection<String> generate(int count) {
//         ArrayList<String> ids = new ArrayList<>();
//         for (int i = 0; i < count; i++) {
//             ids.add(generate());
//         }
//         return ids;
//     }
// 
// }


// Path: core/src/main/java/org/n52/supervisor/api/CheckRunner.java
import java.util.Collection;
import org.n52.supervisor.db.ResultDatabase;
import org.n52.supervisor.id.IdentifierGenerator;
import org.n52.supervisor.id.ShortAlphanumericIdentifierGenerator;

/**
 * ﻿Copyright (C) 2013 - 2014 52°North Initiative for Geospatial Open Source Software GmbH
 *
 * Licensed 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 org.n52.supervisor.api;



/**
 * @author Daniel Nüst
 *
 */
public interface CheckRunner {

	final IdentifierGenerator ID_GENERATOR = new ShortAlphanumericIdentifierGenerator();

    public void addResult(CheckResult r);

    public boolean check();

    public Check getCheck();

    public Collection<CheckResult> getResults();
    
    public Collection<CheckResult> getAndClearResults();

    public void notifyFailure();

    public void notifySuccess();

    public void setCheck(Check check) throws UnsupportedCheckException;

    /**
     * @param rd
     *        is required so that the runner can announce its results
     */
    public void setResultDatabase(ResultDatabase rd);