<commit_before>/*********************************************************************************
 * Copyright 2015-present trivago 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 com.trivago.examples;

import com.trivago.triava.logging.TriavaNullLogger;
import com.trivago.triava.time.EstimatorTimeSource;
import com.trivago.triava.time.OffsetTimeSource;
import com.trivago.triava.time.SystemTimeSource;
import com.trivago.triava.time.TimeSource;


/**
 * Examples for creating and using time sources.
 * 
 * @author cesken
 *
 */
public class TimeSourceExample
{
	static final long Y2000 = 946684800;
	public static void main(String[] args)
	{
		offsetTimeSource();
		estimatorTimeSource();
		chainedTimeSource();
	}

	/**
	 * Demonstrate to move the time back 86400 seconds, which is the typical length of a day.
	 */
	private static void offsetTimeSource()
	{
		TimeSource tsToday = new SystemTimeSource();
		TimeSource tsYesterday = new OffsetTimeSource(tsToday.millis() - 86400, tsToday);
		
		compareTimeSources("System", "Offset", tsToday, tsYesterday);
	}

	private static void compareTimeSources(String ts1name, String ts2name, TimeSource ts1, TimeSource ts2)
	{
		System.out.println("--- " + ts1name + " vs " + ts2name + " START ---");
		for (int i=0; i<30; i++)
		{
			long ts1millis = ts1.millis();
			long ts2millis = ts2.millis();
			long millisDiff = ts1millis - ts2millis;
			System.out.printf("diff=%5d %s %d %s %d%n",  millisDiff, ts1name, ts1millis, ts2name, ts2millis);
			//+  ", " + ts1name + "=" + today + ", " + ts2name + "=" + yesterday
			try
			{
				Thread.sleep(25);
			}
			catch (InterruptedException e)
			{
				e.printStackTrace();
			}
		}
		System.out.println("--- " + ts1name + " vs " + ts2name + " END ---");
		System.out.println();
	}

	
	/**
	 * Demonstrates the difference between estimated time and real time
	 */
	private static void estimatorTimeSource()
	{
		TimeSource tsNow = new SystemTimeSource();
		TimeSource tsEstimated = new EstimatorTimeSource(100);
		
		compareTimeSources("System", "Estimator", tsNow, tsEstimated);
	}
	
	/**
	 * Demonstrates TimeSource chaining. Decorates one TimeSource with another.
	 */
	private static void chainedTimeSource()
	{
		TimeSource tsNow = new SystemTimeSource();
		TimeSource tsMinus1Day = new OffsetTimeSource(tsNow.millis() - 86400, tsNow); // yesterday = - 24h
		TimeSource tsMinus1DayEst = new EstimatorTimeSource(tsMinus1Day, 100, new TriavaNullLogger()); // -24h and estimating
		
		compareTimeSources("System", "Chained", tsNow, tsMinus1DayEst);
		
	}


}<commit_msg>SRT-20268 Add example for TimeSource usage.<commit_after>/*********************************************************************************
 * Copyright 2015-present trivago 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 com.trivago.examples;

import com.trivago.triava.logging.TriavaNullLogger;
import com.trivago.triava.time.EstimatorTimeSource;
import com.trivago.triava.time.OffsetTimeSource;
import com.trivago.triava.time.SystemTimeSource;
import com.trivago.triava.time.TimeSource;


/**
 * Examples for creating and using time sources.
 * 
 * @author cesken
 *
 */
public class TimeSourceExample
{
	static final long Y2000 = 946684800;
	public static void main(String[] args)
	{
		offsetTimeSource();
		estimatorTimeSource();
		chainedTimeSource();
	}

	/**
	 * Demonstrate to move the time back 86400 seconds, which is the typical length of a day.
	 */
	private static void offsetTimeSource()
	{
		TimeSource tsToday = new SystemTimeSource();
		TimeSource tsYesterday = new OffsetTimeSource(tsToday.millis() - 86400, tsToday);
		
		compareTimeSources("System", "Offset", tsToday, tsYesterday);
	}

	/**
	 * Demonstrates the difference between estimated time and real time
	 */
	private static void estimatorTimeSource()
	{
		TimeSource tsNow = new SystemTimeSource();
		TimeSource tsEstimated = new EstimatorTimeSource(100);
		
		compareTimeSources("System", "Estimator", tsNow, tsEstimated);
	}
	
	/**
	 * Demonstrates TimeSource chaining. Decorates one TimeSource with another.
	 */
	private static void chainedTimeSource()
	{
		TimeSource tsNow = new SystemTimeSource();
		TimeSource tsMinus1Day = new OffsetTimeSource(tsNow.millis() - 86400, tsNow); // yesterday = - 24h
		TimeSource tsMinus1DayEst = new EstimatorTimeSource(tsMinus1Day, 100, new TriavaNullLogger()); // -24h and estimating
		
		compareTimeSources("System", "Chained", tsNow, tsMinus1DayEst);
		
	}

	/**
	 * Shows the time and time differences between two TimeSource implementations 
	 */
	private static void compareTimeSources(String ts1name, String ts2name, TimeSource ts1, TimeSource ts2)
	{
		System.out.println("--- " + ts1name + " vs " + ts2name + " START ---");
		for (int i=0; i<30; i++)
		{
			long ts1millis = ts1.millis();
			long ts2millis = ts2.millis();
			long millisDiff = ts1millis - ts2millis;
			System.out.printf("diff=%5d %s %d %s %d%n",  millisDiff, ts1name, ts1millis, ts2name, ts2millis);
			try
			{
				Thread.sleep(25);
			}
			catch (InterruptedException e)
			{
				e.printStackTrace();
			}
		}
		System.out.println("--- " + ts1name + " vs " + ts2name + " END ---");
		System.out.println();
	}

	

}
{'fraction_non_alphanumeric': 0.1569731081926204, 'fraction_numerical': 0.0242338961851157, 'mean_word_length': 4.1547139403706685, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1790745', 'n_tokens_mistral': 2165, 'n_tokens_neox': 1896, 'n_words': 559}