package bt.data.range;

import bt.data.BlockSet;
import bt.data.DataRange;

import java.util.function.Function;

/**
 * @since 1.3
 */
public class Ranges {

    /**
     * @since 1.3
     */
    public static <T extends Range<T>> BlockRange<T> blockRange(T range, long blockSize) {
        return new BlockRange<>(range, blockSize);
    }

    /**
     * @since 1.3
     */
    public static <T extends Range<T>> Range<T> synchronizedRange(T range) {
        return new SynchronizedRange<>(range);
    }

    /**
     * @since 1.3
     */
    public static <T extends Range<T>> Range<BlockRange<T>> synchronizedBlockRange(T range, long blockSize) {
        return new SynchronizedRange<>(blockRange(range, blockSize));
    }

    /**
     * @since 1.3
     */
    public static <T extends Range<T>> DataRange synchronizedDataRange(T range, Function<T, DataRange> converter) {
        SynchronizedRange<T> synchronizedRange = new SynchronizedRange<>(range);
        return new SynchronizedDataRange<>(synchronizedRange, converter::apply);
    }

    /**
     * @since 1.3
     */
    public static DataRange synchronizedDataRange(BlockRange<DataRange> range) {
        SynchronizedRange<BlockRange<DataRange>> synchronizedRange = new SynchronizedRange<>(range);
        return new SynchronizedDataRange<>(synchronizedRange, BlockRange::getDelegate);
    }

    /**
     * @since 1.3
     */
    public static DataRange synchronizedDataRange(DataRange range) {
        SynchronizedRange<DataRange> synchronizedRange = new SynchronizedRange<>(range);
        return new SynchronizedDataRange<>(synchronizedRange, Function.identity());
    }

    /**
     * @since 1.3
     */
    public static BlockSet synchronizedBlockSet(BlockSet blockSet) {
        return new SynchronizedBlockSet(blockSet);
    }

    /**
     * @since 1.3
     */
    public static DataRange dataRange(BlockRange<DataRange> range) {
        return DelegatingDataRange.range(range, DelegatingRange::getDelegate);
    }
}

@@ -241,33 +241,40 @@
 e%3CT%3E blockRange(
-T
+Range%3CT%3E
  range, long blo
@@ -437,25 +437,32 @@
 onizedRange(
-T
+Range%3CT%3E
  range) %7B%0A  
@@ -569,49 +569,17 @@
 tic 
-%3CT extends Range%3CT%3E%3E Range%3CBlock
+Data
 Range
-%3CT%3E%3E
  syn
@@ -591,207 +591,46 @@
 ized
-Block
+Data
 Range(
-T range, long blockSize) %7B%0A        return new SynchronizedRange%3C%3E(block
+Data
 Range
-(
+ 
 range
-, blockSize));%0A    %7D%0A%0A    /**%0A     * @since 1.3%0A     */%0A    public static %3CT extends Range%3CT%3E%3E DataRange s
+) %7B%0A        S
 ynch
@@ -640,90 +640,23 @@
 ized
-Data
 Range
-(T range, Function%3CT, DataRange%3E converter) %7B%0A        SynchronizedRange%3CT
+%3CDataRange
 %3E sy
@@ -771,24 +771,27 @@
 ge, 
-converter::apply
+Function.identity()
 );%0A 
@@ -854,37 +854,25 @@
 c DataRange 
-synchronizedD
+d
 ataRange(Blo
@@ -911,168 +911,58 @@
     
-SynchronizedRange%3CBlockRange%3CDataRange%3E%3E synchronizedRange = new SynchronizedR
+return DelegatingDataRange.r
 ange
-%3C%3E
 (range
-);%0A        return new SynchronizedDataRange%3C%3E(synchronizedRange, Block
+, Delegating
 Rang
@@ -1061,32 +1061,43 @@
 onizedDataRange(
+BlockRange%3C
 DataRange range)
@@ -1081,32 +1081,33 @@
 kRange%3CDataRange
+%3E
  range) %7B%0A      
@@ -1118,32 +1118,43 @@
 nchronizedRange%3C
+BlockRange%3C
 DataRange%3E synch
@@ -1139,32 +1139,33 @@
 Range%3CDataRange%3E
+%3E
  synchronizedRan
@@ -1263,35 +1263,39 @@
 dRange, 
-Function.identity()
+BlockRange::getDelegate
 );%0A    %7D
@@ -1459,196 +1459,7 @@
    %7D
-%0A%0A    /**%0A     * @since 1.3%0A     */%0A    public static DataRange dataRange(BlockRange%3CDataRange%3E range) %7B%0A        return DelegatingDataRange.range(range, DelegatingRange::getDelegate);%0A    %7D
 %0A%7D%0A

Generalize byte range API