List of usage examples for java.lang Integer signum
public static int signum(int i)
From source file:MainClass.java
public static void main(String args[]) { System.out.println(Integer.signum(10)); System.out.println(Integer.signum(-10)); System.out.println(Integer.signum(0)); }
From source file:Main.java
public static void main(String[] args) { // returns 1 as int value is greater than 1 System.out.println(Integer.signum(5)); // returns -1 as int value is less than 1 System.out.println(Integer.signum(-5)); // returns 0 as int value is equal to 0 System.out.println(Integer.signum(0)); }
From source file:com.google.uzaygezen.core.BitVectorTest.java
private void checkCompareTo(Function<Integer, BitVector> factory1, Function<Integer, BitVector> factory2) { for (int j = 0; j < 128; j++) { BitVector b = factory1.apply(j); boolean[] bBits = new boolean[j]; for (int k = 0; k < j; ++k) { bBits[k] = random.nextBoolean(); }/*from w w w . jav a 2 s .co m*/ for (int k = 0; k < j; ++k) { b.set(k, bBits[k]); } BitVector c = factory2.apply(j); boolean[] cBits = new boolean[j]; for (int k = 0; k < j; ++k) { cBits[k] = random.nextBoolean(); } for (int k = 0; k < j; ++k) { c.set(k, cBits[k]); } int cmp = b.compareTo(c); int k = j; while (--k != -1 && bBits[k] == cBits[k]) { } int expected = (k == -1) ? 0 : Boolean.compare(bBits[k], cBits[k]); Assert.assertEquals(Integer.signum(expected), Integer.signum(cmp)); Assert.assertEquals(Integer.signum(cmp), -Integer.signum(-cmp)); Assert.assertEquals(Integer.signum(cmp), b.toBigInteger().compareTo(c.toBigInteger())); } }
From source file:eu.scidipes.toolkits.palibrary.impl.FormFieldImpl.java
/** * @param o//w w w . ja v a 2 s . co m * @return * @see java.lang.Comparable#compareTo(java.lang.Object) */ @Override public int compareTo(final FormField o) { final int left = getFieldID() != null ? getFieldID().intValue() : Integer.MAX_VALUE; final int right = o.getFieldID() != null ? o.getFieldID().intValue() : Integer.MIN_VALUE; return Integer.signum(left - right); }
From source file:com.google.uzaygezen.core.LongBitVector.java
@Override public int compareTo(BitVector o) { checkSize(o);//from w ww. j ava2 s . co m final int cmp; // optimisation if (o.size() <= 64) { // 0, positives, Long.MAX_VALUE, Long.MIN_VALUE, negatives, -1 long x = data + Long.MIN_VALUE; long y = o.toExactLong() + Long.MIN_VALUE; cmp = Long.compare(x, y); assert Integer.signum(cmp) == Integer .signum(BitSetComparator.INSTANCE.compare(toBitSet(), o.toBitSet())); } else { cmp = BitSetComparator.INSTANCE.compare(toBitSet(), o.toBitSet()); } return cmp; }
From source file:io.github.bonigarcia.wdm.BrowserManager.java
public Integer versionCompare(String str1, String str2) { String[] vals1 = str1.replaceAll("v", "").split("\\."); String[] vals2 = str2.replaceAll("v", "").split("\\."); int i = 0;/*from ww w . j ava 2 s. c o m*/ while (i < vals1.length && i < vals2.length && vals1[i].equals(vals2[i])) { i++; } if (i < vals1.length && i < vals2.length) { int diff = Integer.valueOf(vals1[i]).compareTo(Integer.valueOf(vals2[i])); return Integer.signum(diff); } else { return Integer.signum(vals1.length - vals2.length); } }
From source file:org.geowebcache.service.wms.WMSTileFuser.java
protected void determineCanvasLayout() { // Find the spatial extent of the tiles needed to cover the desired extent srcRectangle = gridSubset.getCoverageIntersection(srcIdx, reqBounds); srcBounds = gridSubset.boundsFromRectangle(srcRectangle); // We now have the complete area, lets figure out our offsets // Positive means that there is blank space to the first tile, // negative means we will not use the entire tile boundOfs.left = srcBounds.getMinX() - reqBounds.getMinX(); boundOfs.bottom = srcBounds.getMinY() - reqBounds.getMinY(); boundOfs.right = reqBounds.getMaxX() - srcBounds.getMaxX(); boundOfs.top = reqBounds.getMaxY() - srcBounds.getMaxY(); canvasSize[0] = (int) Math.round(reqBounds.getWidth() / this.srcResolution); canvasSize[1] = (int) Math.round(reqBounds.getHeight() / this.srcResolution); PixelOffsets naiveOfs = new PixelOffsets(); // Calculate the corresponding pixel offsets. We'll stick to sane, // i.e. bottom left, coordinates at this point naiveOfs.left = (int) Math.round(boundOfs.left / this.srcResolution); naiveOfs.bottom = (int) Math.round(boundOfs.bottom / this.srcResolution); naiveOfs.right = (int) Math.round(boundOfs.right / this.srcResolution); naiveOfs.top = (int) Math.round(boundOfs.top / this.srcResolution); // Find the offsets on the opposite sides. This is dependent of how the first two were rounded. // First, find a tile boundary near the canvas edge, then make sure it's on the correct // side to match the corresponding boundOfs, then take the modulo of the naive rounding // based on the boundOfs, then subtract the two and apply the difference to the boundOfs. int tileWidth = this.gridSubset.getTileWidth(); int tileHeight = this.gridSubset.getTileHeight(); canvOfs.left = naiveOfs.left;/*ww w.j a v a2s. co m*/ canvOfs.bottom = naiveOfs.bottom; canvOfs.right = (canvasSize[0] - canvOfs.left) % tileWidth; // Find nearby tile boundary canvOfs.right = (Integer.signum(naiveOfs.right) * tileWidth + canvOfs.right) % tileWidth; // Ensure same sign as naive calculation canvOfs.right = canvOfs.right - (naiveOfs.right % tileWidth) + naiveOfs.right; // Find adjustment from naive and apply to naive calculation canvOfs.top = (canvasSize[1] - canvOfs.bottom) % tileHeight; // Find nearby tile boundary canvOfs.top = (Integer.signum(naiveOfs.top) * tileHeight + canvOfs.top) % tileHeight; // Ensure same sign as naive calculation canvOfs.top = canvOfs.top - (naiveOfs.top % tileHeight) + naiveOfs.top; // Find adjustment from naive and apply to naive calculation //postconditions assert Math.abs(canvOfs.left - naiveOfs.left) <= 1; assert Math.abs(canvOfs.bottom - naiveOfs.bottom) <= 1; assert Math.abs(canvOfs.right - naiveOfs.right) <= 1; assert Math.abs(canvOfs.top - naiveOfs.top) <= 1; if (log.isDebugEnabled()) { log.debug("intersection rectangle: " + Arrays.toString(srcRectangle)); log.debug("intersection bounds: " + srcBounds + " (" + reqBounds + ")"); log.debug("Bound offsets: " + Arrays .toString(new double[] { boundOfs.left, boundOfs.bottom, boundOfs.right, boundOfs.top })); log.debug("Canvas size: " + Arrays.toString(canvasSize) + "(" + reqWidth + "," + reqHeight + ")"); log.debug("Canvas offsets: " + Arrays.toString(new int[] { canvOfs.left, canvOfs.bottom, canvOfs.right, canvOfs.top })); } }
From source file:gobblin.runtime.local.LocalJobManager.java
/** * Restore the lastJobIdMap.// ww w. ja v a 2s . co m */ private void restoreLastJobIdMap() throws IOException { FileSystem fs = FileSystem.get( URI.create(this.properties.getProperty(ConfigurationKeys.STATE_STORE_FS_URI_KEY)), new Configuration()); // Root directory of task states store Path taskStateStoreRootDir = new Path( this.properties.getProperty(ConfigurationKeys.STATE_STORE_ROOT_DIR_KEY)); if (!fs.exists(taskStateStoreRootDir)) { return; } // List subdirectories (one for each job) under the root directory FileStatus[] rootStatuses = fs.listStatus(taskStateStoreRootDir); if (rootStatuses == null || rootStatuses.length == 0) { return; } LOG.info("Restoring the mapping between jobs and IDs of their last runs"); for (FileStatus status : rootStatuses) { // List the task states files under each subdirectory corresponding to a job FileStatus[] statuses = fs.listStatus(status.getPath(), new PathFilter() { @Override public boolean accept(Path path) { return !path.getName().startsWith("current") && path.getName().endsWith(TASK_STATE_STORE_TABLE_SUFFIX); } }); if (statuses == null || statuses.length == 0) { continue; } // Sort the task states files by timestamp in descending order Arrays.sort(statuses, new Comparator<FileStatus>() { @Override public int compare(FileStatus fileStatus1, FileStatus fileStatus2) { String fileName1 = fileStatus1.getPath().getName(); String taskId1 = fileName1.substring(0, fileName1.indexOf('.')); String fileName2 = fileStatus2.getPath().getName(); String taskId2 = fileName2.substring(0, fileName2.indexOf('.')); Long ts1 = Long.parseLong(taskId1.substring(taskId1.lastIndexOf('_') + 1)); Long ts2 = Long.parseLong(taskId2.substring(taskId2.lastIndexOf('_') + 1)); return -Integer.signum(ts1.compareTo(ts2)); } }); // Each subdirectory is for one job, and the directory name is the job name. String jobName = status.getPath().getName(); // The first task states file after sorting has the latest timestamp String fileName = statuses[0].getPath().getName(); String lastJobId = fileName.substring(0, fileName.indexOf('.')); LOG.info(String.format("Restored last job ID %s for job %s", lastJobId, jobName)); this.lastJobIdMap.put(jobName, lastJobId); } }
From source file:com.microfocus.application.automation.tools.octane.executor.UftTestDiscoveryDispatcher.java
/** * Compares two version strings.//ww w.j a va 2s. c o m * <p> * Use this instead of String.compareTo() for a non-lexicographical * comparison that works for version strings. e.g. "1.10".compareTo("1.6"). * * @param str1 a string of ordinal numbers separated by decimal points. * @param str2 a string of ordinal numbers separated by decimal points. * @return The result is a negative integer if str1 is _numerically_ less than str2. * The result is a positive integer if str1 is _numerically_ greater than str2. * The result is zero if the strings are _numerically_ equal. * @note It does not work if "1.10" is supposed to be equal to "1.10.0". */ private static Integer versionCompare(String str1, String str2) { String[] vals1 = str1.split("\\."); String[] vals2 = str2.split("\\."); int i = 0; // set index to first non-equal ordinal or length of shortest version string while (i < vals1.length && i < vals2.length && vals1[i].equals(vals2[i])) { i++; } // compare first non-equal ordinal number if (i < vals1.length && i < vals2.length) { int diff = Integer.valueOf(vals1[i]).compareTo(Integer.valueOf(vals2[i])); return Integer.signum(diff); } // the strings are equal or one string is a substring of the other // e.g. "1.2.3" = "1.2.3" or "1.2.3" < "1.2.3.4" else { return Integer.signum(vals1.length - vals2.length); } }
From source file:com.spotify.reaper.cassandra.JmxProxy.java
/** * NOTICE: This code is loosely based on StackOverflow answer: * http://stackoverflow.com/questions/6701948/efficient-way-to-compare-version-strings-in-java * * Compares two version strings./*ww w . j a va2 s.c o m*/ * * Use this instead of String.compareTo() for a non-lexicographical * comparison that works for version strings. e.g. "1.10".compareTo("1.6"). * * @param str1 a string of ordinal numbers separated by decimal points. * @param str2 a string of ordinal numbers separated by decimal points. * @return The result is a negative integer if str1 is _numerically_ less than str2. * The result is a positive integer if str1 is _numerically_ greater than str2. * The result is zero if the strings are _numerically_ equal. * It does not work if "1.10" is supposed to be equal to "1.10.0". */ public static Integer versionCompare(String str1, String str2) throws ReaperException { try { String cleanedUpStr1 = str1.split(" ")[0].replaceAll("[-_~]", "."); String cleanedUpStr2 = str2.split(" ")[0].replaceAll("[-_~]", "."); String[] parts1 = cleanedUpStr1.split("\\."); String[] parts2 = cleanedUpStr2.split("\\."); int i = 0; // set index to first non-equal ordinal or length of shortest version string while (i < parts1.length && i < parts2.length) { try { Integer.parseInt(parts1[i]); Integer.parseInt(parts2[i]); } catch (NumberFormatException ex) { if (i == 0) { throw ex; // just comparing two non-version strings should fail } // first non integer part, so let's just stop comparison here and ignore the rest i--; break; } if (parts1[i].equals(parts2[i])) { i++; continue; } break; } // compare first non-equal ordinal number if (i < parts1.length && i < parts2.length) { int diff = Integer.valueOf(parts1[i]).compareTo(Integer.valueOf(parts2[i])); return Integer.signum(diff); } // the strings are equal or one string is a substring of the other // e.g. "1.2.3" = "1.2.3" or "1.2.3" < "1.2.3.4" else { return Integer.signum(parts1.length - parts2.length); } } catch (Exception ex) { LOG.error("failed comparing strings for versions: '{}' '{}'", str1, str2); throw new ReaperException(ex); } }