blob: b9a0b6aa2959878707ea0251ddeab5e8b2eb03c0 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2004, 2007 Boeing.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Boeing - initial API and implementation
*******************************************************************************/
package org.eclipse.osee.ote.message.condition;
import org.eclipse.osee.ote.message.elements.DiscreteElement;
public class PulseCondition<T extends Comparable<T>> extends AbstractCondition implements IDiscreteElementCondition<T> {
private final int maxPulses;
private final DiscreteElement<T> element;
private final T pulsedValue;
private final T nonPulsedValue;
private int pulses = 0;
private T lastValue;
public PulseCondition(DiscreteElement<T> element, T pulsedValue, T nonPulsedValue, int numPulses) {
this.element = element;
this.pulsedValue = element.elementMask(pulsedValue);
this.nonPulsedValue = element.elementMask(nonPulsedValue);
this.maxPulses = numPulses;
}
public PulseCondition(DiscreteElement<T> element, T pulsedValue, T nonPulsedValue) {
this(element, pulsedValue, nonPulsedValue, 2);
}
@Override
public T getLastCheckValue() {
return lastValue;
}
@Override
public boolean check() {
lastValue = element.getValue();
if (lastValue.equals(pulsedValue)) {
pulses++;
} else if (lastValue.equals(nonPulsedValue)) {
if( pulses >= maxPulses ) {
return true;
} else {
pulses = 0;
}
}
return false;
}
public int getPulses() {
return pulses;
}
}