public void insert(long time, double value) {
    timeWindow.push(time);
    valueWindow.push(value);
    if (startTime < 0) {
      startTime = time;
    }
    switch (state) {
      case 0:
        if (timeWindow.getSize() >= getWindowSize()) {
          linearRegress(0, getWindowSize());
          double alpha = regression.getRSquare();
          if (Double.isNaN(alpha) || alpha > THRESHOLD) {
            missingStartIndex = 0;
            state = 2;
          } else {
            regression.clear();
            state = 1;
          }
        }
        break;
      case 1:
        if (timeWindow.getSize() >= getWindowSize() * 2) {
          linearRegress(getWindowSize(), getWindowSize() * 2);
          double alpha = regression.getRSquare();
          if (Double.isNaN(alpha) || alpha > THRESHOLD) {
            missingStartIndex = backExtend();
            state = 2;
          } else {
            for (int i = 0; i < getWindowSize(); i++) {
              timeBuffer.push(timeWindow.pop());
              labelBuffer.push(false);
              valueWindow.pop();
            }
            regression.clear();
            state = 1;
          }
        }
        break;
      case 2:
        regression.addData((double) time - startTime, value);
        double alpha = regression.getRSquare();
        if ((horizon && value != standard) || (!horizon && alpha < THRESHOLD)) {
          int missingEndIndex = timeWindow.getSize() - 1;
          for (int i = 0; i < missingStartIndex; i++) {
            timeBuffer.push(timeWindow.pop());
            labelBuffer.push(false);
            valueWindow.pop();
          }
          boolean label = missingEndIndex - missingStartIndex > minLength;
          for (int i = missingStartIndex; i < missingEndIndex; i++) {
            timeBuffer.push(timeWindow.pop());
            labelBuffer.push(label);
            valueWindow.pop();
          }
          regression.clear();
          state = 0;
        }
        break;
      default:
        break;
    }
  }
Streaming anomaly detection for anomalies of data missing.
library-udf/src/main/java/org/apache/iotdb/library/anomaly/util/StreamMissDetector.java