Example usage for java.lang Math max

List of usage examples for java.lang Math max

Introduction

In this page you can find the example usage for java.lang Math max.

Prototype

@HotSpotIntrinsicCandidate
public static double max(double a, double b) 

Source Link

Document

Returns the greater of two double values.

Usage

From source file:LamportBasicVersion.java

public static void main(String[] args) throws FileNotFoundException, IOException, InterruptedException {
    totalMessageCount = 0;/*from   w  w  w .j av a2  s .co m*/
    //For parsing the file and storing the information
    String line;
    String configurationFile = "configuration.txt";
    int lineCountInFile = 0;
    myProcessId = Integer.parseInt(args[0]);
    FileReader fileReader = new FileReader(configurationFile);
    BufferedReader bufferedReader = new BufferedReader(fileReader);
    while ((line = bufferedReader.readLine()) != null) {
        if ((!(line.startsWith("#"))) && (!(line.isEmpty()))) {
            lineCountInFile = lineCountInFile + 1;
            String[] splitLine = line.split(" ");
            switch (lineCountInFile) {
            case 1:
                numberOfProcesses = Integer.parseInt(splitLine[0]);
                interRequestDelay = Integer.parseInt(splitLine[1]);
                csExecutionTime = Integer.parseInt(splitLine[2]);
                maxNumberOfRequest = Integer.parseInt(splitLine[3]);
                machineNames = new String[Integer.parseInt(splitLine[0])];
                portNumbers = new int[Integer.parseInt(splitLine[0])];
                break;
            default:
                machineNames[lineCountInFile - 2] = splitLine[1];
                portNumbers[lineCountInFile - 2] = Integer.parseInt(splitLine[2]);
                break;
            }
        }
    }
    conditionArray = new int[numberOfProcesses];
    finishFlagArray = new int[numberOfProcesses];
    //Initializing vector class
    VectorClass.initialize(numberOfProcesses);
    //Fill the arrays with zero false value
    for (int o = 0; o < numberOfProcesses; o++) {
        conditionArray[o] = 0;
        finishFlagArray[o] = 0;
    }
    // Write output to file
    filename = filename + Integer.toString(myProcessId) + ".out";
    file = new File(filename);
    file.createNewFile();
    writer = new FileWriter(file);
    // Write clocks to file
    filenameClock = filenameClock + Integer.toString(myProcessId) + ".out";
    fileClock = new File(filenameClock);
    fileClock.createNewFile();
    //writerClock = new FileWriter(fileClock);
    fw = new FileWriter(fileClock);
    bw = new BufferedWriter(fw);
    //
    // Expo mean insert
    csExecutionExpoDelay = new ExponentialDistribution(csExecutionTime);
    interRequestExpoDelay = new ExponentialDistribution(interRequestDelay);
    //
    System.out.println("********************************************************");
    System.out.println("My process id : " + myProcessId);
    System.out.println("Number of processes : " + numberOfProcesses);
    System.out.println("Inter-request delay : " + interRequestDelay);
    System.out.println("Critical section execution time : " + csExecutionTime);
    System.out.println("Maximum number of request : " + maxNumberOfRequest);
    System.out.println(
            "My process name : " + machineNames[myProcessId] + " My port number : " + portNumbers[myProcessId]);
    for (int i = 0; i < numberOfProcesses; i++) {
        System.out.println("Process name : " + machineNames[i] + " Port number : " + portNumbers[i]);
    }
    System.out.println("********************************************************");
    //For hosting server localhost
    SctpServerChannel sctpServerChannel = SctpServerChannel.open();
    InetSocketAddress serverAddr = new InetSocketAddress(portNumbers[myProcessId]);
    sctpServerChannel.bind(serverAddr);
    System.out.println("********************************************************");
    System.out.println("Local server hosted");
    System.out.println("********************************************************");
    //For creating neighbor SCTP channels
    Thread.sleep(30000);
    socketAddress = new SocketAddress[numberOfProcesses];
    sctpChannel = new SctpChannel[numberOfProcesses];
    System.out.println("********************************************************");
    System.out.println("Neighbor channels created");
    System.out.println("********************************************************");
    //Thread spanned for generating critical section request
    new Thread(new LamportBasicVersion()).start();
    //while loop to receive all the requests and other messages
    while (true) {
        try (SctpChannel sctpChannelFromClient = sctpServerChannel.accept()) {
            mutex.acquire();
            byteBufferFromNeighbor.clear();
            String receiveMessage;
            MessageInfo messageInfoFromNeighbor = sctpChannelFromClient.receive(byteBufferFromNeighbor, null,
                    null);
            //System.out.println("Raw Message : " + messageInfoFromNeighbor);
            receiveMessage = byteToString(byteBufferFromNeighbor, messageInfoFromNeighbor);
            //write to file start
            writer.write("Received Message : " + receiveMessage);
            writer.write("\n");
            writer.flush();
            //write to file end
            System.out.println("Received Message : " + receiveMessage);
            if (receiveMessage.contains("Request")) {
                String[] parseMessage = receiveMessage.split("-");
                lamportClock = Math.max(lamportClock, Integer.parseInt(parseMessage[3])) + 1;
                //vector clock update
                String[] stringNumericalTimestamp = parseMessage[4].split(",");
                int[] numericalTimestamp = new int[stringNumericalTimestamp.length];
                for (int d = 0; d < stringNumericalTimestamp.length; d++) {
                    numericalTimestamp[d] = Integer.parseInt(stringNumericalTimestamp[d]);
                }
                VectorClass.update(myProcessId, numericalTimestamp);
                //
                int requestMade = Integer.parseInt(parseMessage[3] + parseMessage[1]);
                queue.add(requestMade);
                //Send reply messages to that process for entering CS
                lamportClock++;
                //vector clock construction
                int[] vector = VectorClass.increment(myProcessId);
                String vectorClockConstruction = "";
                for (int g = 0; g < vector.length; g++) {
                    if (g == 0) {
                        vectorClockConstruction = vectorClockConstruction + Integer.toString(vector[g]);
                    } else {
                        vectorClockConstruction = vectorClockConstruction + "," + Integer.toString(vector[g]);
                    }
                }
                //
                for (int k = 0; k < numberOfProcesses; k++) {
                    if (k == Integer.parseInt(parseMessage[1])) {
                        try {
                            byteBufferToNeighbor.clear();
                            initializeChannels();
                            sctpChannel[k].connect(socketAddress[k]);
                            String sendMessage = "Reply from Process-" + myProcessId + "-"
                                    + Integer.toString(requestMade) + "-" + lamportClock + "-"
                                    + vectorClockConstruction;
                            System.out.println("Message sent is : " + sendMessage);
                            //write to file start
                            writer.write("Message sent is : " + sendMessage);
                            writer.write("\n");
                            writer.flush();
                            //write to file end
                            MessageInfo messageInfoToNeighbor = MessageInfo.createOutgoing(null, 0);
                            byteBufferToNeighbor.put(sendMessage.getBytes());
                            byteBufferToNeighbor.flip();
                            sctpChannel[k].send(byteBufferToNeighbor, messageInfoToNeighbor);
                            totalMessageCount++;
                            sctpChannel[k].close();
                        } catch (IOException ex) {
                            Logger.getLogger(LamportBasicVersion.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }
            } else if (receiveMessage.contains("Reply")) {
                conditionArray[myProcessId] = 1;
                String[] parseMessage = receiveMessage.split("-");
                lamportClock = Math.max(lamportClock, Integer.parseInt(parseMessage[3])) + 1;
                //vector clock update
                String[] stringNumericalTimestamp = parseMessage[4].split(",");
                int[] numericalTimestamp = new int[stringNumericalTimestamp.length];
                for (int d = 0; d < stringNumericalTimestamp.length; d++) {
                    numericalTimestamp[d] = Integer.parseInt(stringNumericalTimestamp[d]);
                }
                VectorClass.update(myProcessId, numericalTimestamp);
                //
                conditionArray[Integer.parseInt(parseMessage[1])] = 1;
                int count = 0;
                for (int v = 0; v < numberOfProcesses; v++) {
                    if (conditionArray[v] == 1) {
                        count = count + 1;
                    }
                }
                if (count == numberOfProcesses) {
                    L1ConditionFlag = 1;
                    System.out.println("Inside L1");
                    blockingQueue.put("L1");
                    //Clearing condition array after receiving all REPLY
                    for (int z = 0; z < numberOfProcesses; z++) {
                        conditionArray[z] = 0;
                    }
                    if (L2ConditionFlag == 0 && outstandingRequest == 1) {
                        Integer[] queueArray = new Integer[queue.size()];
                        queue.toArray(queueArray);
                        Arrays.sort(queueArray);
                        if (queueArray[0] == currentRequestBeingServed) {
                            System.out.println("Inside L2");
                            L2ConditionFlag = 1;
                            blockingQueue.put("L2");
                        }
                    }
                }
            } else if (receiveMessage.contains("Release")) {
                int present = 0;
                int delete = 0;
                String[] parseMessage = receiveMessage.split("-");
                lamportClock = Math.max(lamportClock, Integer.parseInt(parseMessage[3])) + 1;
                //vector clock update
                String[] stringNumericalTimestamp = parseMessage[4].split(",");
                int[] numericalTimestamp = new int[stringNumericalTimestamp.length];
                for (int d = 0; d < stringNumericalTimestamp.length; d++) {
                    numericalTimestamp[d] = Integer.parseInt(stringNumericalTimestamp[d]);
                }
                VectorClass.update(myProcessId, numericalTimestamp);
                //
                if (queue.size() != 0) {
                    Integer[] queueArray = new Integer[queue.size()];
                    queue.toArray(queueArray);
                    Arrays.sort(queueArray);
                    for (int a = 0; a < queueArray.length; a++) {
                        if (queueArray[a] == Integer.parseInt(parseMessage[2])) {
                            present = 1;
                            delete = a;
                        }
                    }
                    if (present == 1) {
                        for (int s = 0; s <= delete; s++) {
                            queue.remove();
                        }
                    }
                }
                if (L2ConditionFlag == 0 && outstandingRequest == 1) {
                    if (queue.size() != 0) {
                        Integer[] queueArray1 = new Integer[queue.size()];
                        queue.toArray(queueArray1);
                        Arrays.sort(queueArray1);
                        if (currentRequestBeingServed == queueArray1[0]) {
                            L2ConditionFlag = 1;
                            System.out.println("Inside L2");
                            blockingQueue.put("L2");
                        }
                    }
                }
            } else if (receiveMessage.contains("Finish")) {
                String[] parseMessage = receiveMessage.split("-");
                lamportClock = Math.max(lamportClock, Integer.parseInt(parseMessage[3])) + 1;
                //vector clock update
                String[] stringNumericalTimestamp = parseMessage[4].split(",");
                int[] numericalTimestamp = new int[stringNumericalTimestamp.length];
                for (int d = 0; d < stringNumericalTimestamp.length; d++) {
                    numericalTimestamp[d] = Integer.parseInt(stringNumericalTimestamp[d]);
                }
                VectorClass.update(myProcessId, numericalTimestamp);
                //
                finishFlagArray[Integer.parseInt(parseMessage[1])] = 1;
                int count = 0;
                for (int v = 0; v < numberOfProcesses; v++) {
                    if (finishFlagArray[v] == 1) {
                        count = count + 1;
                    }
                }
                if (count == numberOfProcesses) {
                    break;
                }
            }
            //logic for other messages
            //Print the queue to check
            System.out.println("********************************************************");
            for (Object item : queue) {
                System.out.print(item);
                System.out.print("\t");
            }
            System.out.println("********************************************************");
        }
        mutex.release();
    }
}

From source file:Main.java

public static float clamp(float value, float max, float min) {
    return Math.max(Math.min(value, min), max);
}

From source file:Main.java

private static String getVoiceTime(long time) {
    long seconds = Math.max((time / 1000), 1);
    long min = seconds / 60;
    long sec = seconds % 60;

    return ((min > 0) ? (String.valueOf(min) + "'") : "") + ((sec >= 0) ? (String.valueOf(sec) + "\"") : "");
}

From source file:Main.java

public static double boundaryRestrict(double f, double e, double d) {
    return Math.max(Math.min(f, d), e);
}

From source file:Main.java

public static int clamp(final int num, final int max, final int min) {
    return Math.max(Math.min(num, max), min);
}

From source file:Main.java

public static boolean isShadeOfGray(int red, int green, int blue) {
    return Math.abs(Math.max(red, Math.max(green, blue)) - Math.min(red, Math.min(green, blue))) < 20;
}

From source file:Main.java

public static int adjust(final int value, final int min, final int max) {
    return Math.min(Math.max(min, value), max);
}

From source file:Main.java

public static double calculateMinZoomLevel(int width, int height, int tileSize) {
    int max = Math.max(width, height);
    int tilesCount = (int) Math.ceil(1d * max / tileSize);
    return Math.ceil(Math.log(tilesCount) / Math.log(2d));
}

From source file:Main.java

public static int maxDigit(int n) {
    return n == 0 ? 0 : Math.max(n % 10, maxDigit(n / 10));
}

From source file:Main.java

public static Float getFloat(float value, float minValue, float maxValue) {
    return Math.min(maxValue, Math.max(minValue, value));
}