package ru.nsu.ccfit.bogush.factory;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class SimplePeriodical implements Periodical {
	private long period;

	private static final String LOGGER_NAME = "SimplePeriodical";
	private static final Logger logger = LogManager.getLogger(LOGGER_NAME);

	public SimplePeriodical(long period) {
		this.period = period;
	}

	private final Object lock = new Object();

	@Override
	public void setPeriod(long period) {
		logger.trace("set period " + period + " milliseconds");
		if (period < 0) {
			logger.error("period < 0");
			throw new IllegalArgumentException("period must be greater than or equal to 0");
		}
		this.period = period;
		lock.notifyAll();
	}

	@Override
	public void waitPeriod() {
		long timeToWait = period;
		long time = System.currentTimeMillis();
		try {
			synchronized (lock) {
				while (timeToWait > 0) {
					lock.wait(timeToWait);
					long newTime = System.currentTimeMillis();
					timeToWait -= newTime - time;
					time = newTime;
				}
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
			logger.error(e);
			System.exit(1);
		}
	}

	@Override
	public long getPeriod() {
		return period;
	}
}

@@ -799,24 +799,52 @@
 t = period;%0A
+%09%09while (timeToWait %3E 0) %7B%0A%09
 %09%09long time 
@@ -879,14 +879,16 @@
 ;%0A%09%09
+%09
 try %7B%0A
+%09
 %09%09%09s
@@ -912,37 +912,8 @@
 ) %7B%0A
-%09%09%09%09while (timeToWait %3E 0) %7B%0A
 %09%09%09%09
@@ -945,21 +945,20 @@
 %09%09%09%09
-long newTime 
+timeToWait -
 = Sy
@@ -985,77 +985,24 @@
 is()
-;%0A%09%09%09%09%09timeToWait -= newTime - time;%0A%09%09%09%09%09time = newTime;%0A%09%09%09%09%7D%0A%09%09%09%7D%0A
+ - time;%0A%09%09%09%09%7D%0A%09
 %09%09%7D 
@@ -1037,16 +1037,17 @@
 e) %7B%0A%09%09%09
+%09
 e.printS
@@ -1059,16 +1059,17 @@
 race();%0A
+%09
 %09%09%09logge
@@ -1083,16 +1083,17 @@
 (e);%0A%09%09%09
+%09
 System.e
@@ -1100,16 +1100,21 @@
 xit(1);%0A
+%09%09%09%7D%0A
 %09%09%7D%0A%09%7D%0A%0A

Rearrange synchronized, try-catch and while-loop blocks