LongRangeSet (long from1, long to1, long from2, long to2, LongRangeType lrType) { long len1 = to1 - from1; // may overflow long len2 = to2 - from2; // may overflow if ((len1 < 0 && len2 < 0) || ((len1 < 0 || len2 < 0) && len1 + len2 + 1 >= 0)) { // total length more than 2^64 return lrType.fullRange(); } long from = from1 + from2; long to = to1 + to2; // Other possible types not supported here yet assert lrType == LongRangeType.INT64 || lrType == LongRangeType.INT32; if (lrType == LongRangeType.INT32) { if (to - from + 1 >= 0x1_0000_0000L) { return INT_RANGE; } from = (int)from; to = (int)to; } if (to < from) { return new RangeSet(new long[]{lrType.min(), to, from, lrType.max()}); } else { return range(from, to); } }