/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.shardingsphere.example.extension.sharding.algortihm.spibased.spi;

import java.util.Collection;
import org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.StandardShardingAlgorithm;

public class SpiBasedShardingAlgorithm implements StandardShardingAlgorithm<Comparable<?>> {

  @Override
  public String doSharding(Collection<String> availableTargetNames,
      PreciseShardingValue<Comparable<?>> shardingValue) {
    for (String each : availableTargetNames) {
      if (each.endsWith(String.valueOf(Integer.valueOf(shardingValue.getValue().toString()) % 2))) {
        return each;
      }
    }
    return null;
  }

  @Override
  public Collection<String> doSharding(Collection<String> availableTargetNames,
      RangeShardingValue<Comparable<?>> shardingValue) {
    return availableTargetNames;
  }

  @Override
  public void init() {
  }

  @Override
  public String getType() {
    return "SPI_BASED";
  }
}

@@ -1180,16 +1180,22 @@
 %0Apublic 
+final 
 class Sp
@@ -1273,16 +1273,18 @@
 %3C?%3E%3E %7B%0A%0A
+  
   @Overr
@@ -1279,32 +1279,34 @@
 %0A%0A    @Override%0A
+  
   public String 
@@ -1308,32 +1308,38 @@
 ring doSharding(
+final 
 Collection%3CStrin
@@ -1362,22 +1362,22 @@
 etNames,
-%0A     
+ final
  Precise
@@ -1418,24 +1418,28 @@
 ingValue) %7B%0A
+    
     for (Str
@@ -1469,24 +1469,30 @@
 getNames) %7B%0A
+      
       if (ea
@@ -1576,24 +1576,32 @@
 )) %25 2))) %7B%0A
+        
         retu
@@ -1609,16 +1609,22 @@
 n each;%0A
+      
       %7D%0A
@@ -1623,26 +1623,34 @@
       %7D%0A    
+ 
-%7D%0A
+   %7D%0A    
     return n
@@ -1654,21 +1654,25 @@
 n null;%0A
+  
   %7D%0A%0A
+  
   @Overr
@@ -1667,32 +1667,34 @@
 %0A%0A    @Override%0A
+  
   public Collect
@@ -1716,16 +1716,22 @@
 harding(
+final 
 Collecti
@@ -1762,22 +1762,22 @@
 etNames,
-%0A     
+ final
  RangeSh
@@ -1816,24 +1816,28 @@
 ingValue) %7B%0A
+    
     return a
@@ -1857,21 +1857,25 @@
 tNames;%0A
+  
   %7D%0A%0A
+  
   @Overr
@@ -1870,32 +1870,34 @@
 %0A%0A    @Override%0A
+  
   public void in
@@ -1903,21 +1903,25 @@
 nit() %7B%0A
+  
   %7D%0A%0A
+  
   @Overr
@@ -1924,16 +1924,18 @@
 verride%0A
+  
   public
@@ -1950,24 +1950,28 @@
 getType() %7B%0A
+    
     return %22
@@ -1982,14 +1982,16 @@
 BASED%22;%0A
+  
   %7D%0A%7D%0A

optimize code