Untitled

 avatar
unknown
plain_text
2 months ago
5.0 kB
3
Indexable
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
public class Main {

    class SensorData {
        String sensorId;
        Double temperature;
        LocalDateTime timestamp;

        public SensorData(String sensorId, Double temperature, LocalDateTime timestamp) {
            this.sensorId = sensorId;
            this.temperature = temperature;
            this.timestamp = timestamp;
        }
    }

    class ListNode {
        SensorData data;
        ListNode previous;
        ListNode next;

        public ListNode(SensorData data, ListNode previous, ListNode next) {
            this.data = data;
            this.previous = previous;
            this.next = next;
        }

        public ListNode getPrevious() { return this.previous; }

        public ListNode getNext() { return this.next; }

        public SensorData getData() { return this.data; }
    }

    class DoubleLinkedList {
        ListNode first;
        ListNode last;

        public DoubleLinkedList() {
            this.first = new ListNode(null, null, null);
            this.last = this.first;
        }

        public ListNode getFirst() { return this.first; }
        public ListNode getLast() { return this.last; }

        public void addAtLast(SensorData data) {
            ListNode newNode = new ListNode(data, this.last, null);
        }
    }

    class SensorRecord<T> {
        DoubleLinkedList fullList = null;
        T startPointer = null;
        T endPointer = null;
        public AtomicBoolean isLocked = false; // will create setter

        public SensorRecord(DoubleLinkedList fullList, T startPointer, T endPointer, Boolean isLocked) {
            this.fullList = fullList;
            this.startPointer = startPointer;
            this.endPointer = endPointer;
            this.isLocked = (isLocked != null) ? isLocked : false;
        }
    }

    Map<String, SensorRecord<ListNode>> allSensorData = new HashMap<>();

    public void addSensorReading(SensorData newSensorReading) {
        String sensorId = newSensorReading.sensorId;
        if (!allSensorData.containsKey(sensorId)) {
            DoubleLinkedList sensorDataList = new DoubleLinkedList();
            SensorRecord<ListNode> sensorRecord = new SensorRecord<>(sensorDataList, sensorDataList.getFirst(), sensorDataList.getLast(), false);
            allSensorData.put(sensorId, sensorRecord);
        }

        SensorRecord<ListNode> currentSensorRecord = allSensorData.get(newSensorReading.sensorId);
        if (!currentSensorRecord.isLocked) {
            currentSensorRecord.isLocked = true;
            currentSensorRecord.fullList.addAtLast(newSensorReading);
            currentSensorRecord.isLocked = false;
        } else {
            // Thread.sleep(100ms)
            addSensorReading(newSensorReading);
        }
    }

    public Double calculateAverage(String sensorId) {
        if (!allSensorData.containsKey(sensorId)) {
            return 0d;
        }
        SensorRecord<ListNode> currentSensorRecord = allSensorData.get(sensorId);
        if (!currentSensorRecord.isLocked) {
            currentSensorRecord.isLocked = true; // will use getter setter
            ListNode lastNode = currentSensorRecord.fullList.getLast();
            LocalDateTime currentTimestamp = LocalDateTime.now();
            LocalDateTime last60Secs = currentTimestamp.minusMinutes(1);
            Double sum = 0d;
            ListNode currentNode = lastNode;
            Integer totalCount = 0;
            while (currentNode.getData().timestamp.isAfter(last60Secs)) {
                totalCount +=1;
                sum += currentNode.getData().temperature; // getter setter
                currentNode = currentNode.getPrevious();
                if (currentNode == null) {
                    break;
                }
            }
            currentSensorRecord.isLocked = false;
            return sum/totalCount;
        } else {
            // Thread.sleep(100ms)
            return calculateAverage(sensorId);
        }
    }

    public static void main(String[] args) {
        //TIP Press <shortcut actionId="ShowIntentionActions"/> with your caret at the highlighted text
        // to see how IntelliJ IDEA suggests fixing it.
        System.out.printf("Hello and welcome!");

        for (int i = 1; i <= 5; i++) {
            //TIP Press <shortcut actionId="Debug"/> to start debugging your code. We have set one <icon src="AllIcons.Debugger.Db_set_breakpoint"/> breakpoint
            // for you, but you can always add more by pressing <shortcut actionId="ToggleLineBreakpoint"/>.
            System.out.println("i = " + i);
        }

        LinkedList
    }
}
Editor is loading...
Leave a Comment