package threadtesting;

public class Data {
	
	/* vector's */
	int [] B;
	int [] C;
	
	/* matrix's */
	int [][] MX;	
	
	/* result vector */
	long [] A;
	
	/* flag's */	
	private boolean thread2FinishedInitialization;
	
	private boolean thread2FinishedCalculation;
	
	private boolean thread1CopiedTheC;
	private boolean thread2CopiedTheC;
		
	private boolean thread2FinishedCalculationA;
	
	
	/* vector size */
	private  int size;
	
	/* constructor */
	public Data(int newSize) {
		size = newSize;
		A = new long[size];				
		thread2FinishedInitialization = false;
		
		thread2FinishedCalculation = false;
		
		thread1CopiedTheC = false;
		thread2CopiedTheC = false;
	}
	//---------------------------------------------------------//
	//---------------------------------------------------------//
	public synchronized void waitForInitializeDataInThread2() {
		try {	
			if(!thread2FinishedInitialization) {
				wait();
			}	
		} 
		catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	//--------------
	public synchronized void initializationDataProcessInThread2Finished() {
		notify();
		thread2FinishedInitialization = true;
		System.out.println("Потік2: Завершено ініціалізацію даних!");
	}
	//--------------
	public synchronized void waitForThread1CopyC() {	
		try {		
			if(!thread1CopiedTheC) {
				wait();
			}			
		} 
		catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	//--------------
	public synchronized void thread1CopiedC() {
		System.out.println("Потік1: Завершено копіювання спільного ресурсу!");
		thread1CopiedTheC = true;
		notify();	
	}
	//--------------
	public synchronized void waitForThread2CopyC() {	
		try {			
			if(!thread2CopiedTheC) {
				wait();
			}			
		} 
		catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	//--------------
	public synchronized void thread2CopiedC() {
		System.out.println("Потік2: Завершено копіювання спільного ресурсу!");
		thread2CopiedTheC = true;
		notify();	
	}
	
		public synchronized void waitForEndOfCalculationInThread2() {
			try {
				if(!thread2FinishedCalculation)
					wait();
			} 
			catch (InterruptedException e) {
				e.printStackTrace();
			}
		}		
	
	
	// methods for send signal's that calculation process is finished!
	public synchronized void calculationProcessInThread2Finished() {
		System.out.println("Потік2: Завершено обрахунок даних!");
		thread2FinishedCalculation = true;
		notify();	
	}	
	//------------
	public synchronized void whaitForThread2FinishedCalculationA() {
		try {
			if(!thread2FinishedCalculationA)
				wait();
		} 
		catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	//-------------------------------------------------------//
	public synchronized int[] getCopyOfC() {
		return C;
	}
	//-------------------------------------------------------//
	/* method for creating new array(with random values) */
	public int [] generateArray(int ii) {
		int [] array = new int[size];
		
		for(int i=0;i<size;i++) {
			array[i] = ii;
		}
		return array;
	}
	
	/* method for creating new matrix(with random values) */
	public int [][] generateMatrix(int ii) {
		int [][] matrix = new int[size][size];
		
		for(int i=0;i<size;i++) {
			for(int j =0; j<size;j++) {
				matrix[i][j] = ii;
			}
		}
		return matrix;
	}
	
	public void printA() {
		System.out.println(" Вектор A :");
		for(int i=0;i<size;i++) {
			System.out.print("  A[");System.out.print(i);System.out.print("]=");
			System.out.print(A[i]);
		}
		System.out.println();
	}
	
	public void PrintB() {
		System.out.println(" Вектор B :");
		for(int i=0;i<size;i++) {
			System.out.print("  B[");System.out.print(i);System.out.print("]=");
			System.out.print(B[i]);
		}
		System.out.println();
	}

	public void PrintC() {
		System.out.println(" Вектор C :");
		for(int i=0;i<size;i++) {
			System.out.print("  C[");System.out.print(i);System.out.print("]=");
			System.out.print(C[i]);
		}
		System.out.println();
	}
	public void PrintMX() {
		System.out.println(" Матриця MX :");
		for(int i=0;i<size;i++) {
			for(int j=0;j<size;j++)
			{
				System.out.print("  MX[");System.out.print(i);System.out.print(",");System.out.print(j);System.out.print("]=");
				System.out.print(MX[i][j]);
			}
			System.out.println();
		}
	}
	

	public int getSize() {
		return size;
	}
	

}
