/*
 * The MIT License (MIT)
 * 
 * Copyright (c) 2014 Jeff Nelson, Cinchapi Software Collective
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.cinchapi.concourse.server.storage;

import org.cinchapi.concourse.Link;
import org.cinchapi.concourse.thrift.Operator;
import org.cinchapi.concourse.thrift.TObject;
import org.cinchapi.concourse.util.Convert;
import org.cinchapi.concourse.util.TStrings;

/**
 * {@link Store} based utility functions.
 * 
 * @author jnelson
 */
public final class Stores {

    /**
     * Perform any necessary normalization on {@code operator} so that it can be
     * properly utilized in {@link Store} methods (i.e. convert a utility
     * operator to a functional one).
     * 
     * @param operator
     * @return the normalized Operator
     */
    public static Operator normalizeOperator(Operator operator) {
        return operator == Operator.LINKS_TO ? Operator.EQUALS : operator;
    }

    /**
     * Perform any necessary normalization on the {@code value} based on the
     * {@code operator}.
     * 
     * @param operator
     * @param values
     * @return the normalized value
     */
    public static TObject normalizeValue(Operator operator, TObject value) {
        try {
        	switch(operator){
        	case REGEX :  
        	case NOT_REGEX:
        		value=Convert.javaToThrift(((String)Convert.thriftToJava(value)).replaceAll(TStrings.REGEX_PERCENT_SIGN_WITHOUT_ESCAPE_CHAR, ".*").replaceAll(TStrings.REGEX_PERCENT_SIGN_WITH_ESCAPE_CHAR, "%"));
        		break;
        	case LINKS_TO:
        		value=Convert.javaToThrift(Link
                        .to(((Number) Convert.thriftToJava(value)).longValue()));
        		break;
        	default :
        		value=value;
        		break;
        	}
            return value;
        }
        catch (ClassCastException e) {
            return value;
        }
    }
}

@@ -2276,15 +2276,19 @@
     
-%09
+    
 switch
+ 
 (ope
@@ -2293,16 +2293,17 @@
 perator)
+ 
 %7B%0A      
@@ -2300,25 +2300,28 @@
 ) %7B%0A        
-%09
+    
 case REGEX :
@@ -2322,21 +2322,19 @@
 EGEX
- :  %0A
+:%0A 
         
 %09cas
@@ -2325,25 +2325,27 @@
 X:%0A         
-%09
+   
 case NOT_REG
@@ -2352,32 +2352,40 @@
 EX:%0A        
-%09%09
+        
 value
-=
+ = 
 Convert.java
@@ -2402,23 +2402,49 @@
 (String)
+ 
 Convert
+%0A                        
 .thriftT
@@ -2460,32 +2460,57 @@
 ue)).replaceAll(
+%0A                        
 TStrings.REGEX_P
@@ -2547,28 +2547,86 @@
 R, %22.*%22)
-.replaceAll(
+%0A                        .replaceAll(%0A                                
 TStrings
@@ -2662,16 +2662,48 @@
 PE_CHAR,
+%0A                               
  %22%25%22));%0A
@@ -2702,34 +2702,40 @@
  %22%25%22));%0A        
-%09%09
+        
 break;%0A        %09
@@ -2733,17 +2733,20 @@
         
-%09
+    
 case LIN
@@ -2760,24 +2760,32 @@
         
+      
-%09%09
+  
 value
-=
+ = 
 Convert.
@@ -2801,16 +2801,37 @@
 ift(Link
+.to(((Number) Convert
 %0A       
@@ -2847,37 +2847,16 @@
         
-.to(((Number) Convert
 .thriftT
@@ -2884,34 +2884,40 @@
 ue()));%0A        
+    
-%09%09
+    
 break;%0A        %09
@@ -2919,17 +2919,19 @@
     
+   
-%09
+ 
 default
- 
 :%0A  
@@ -2940,31 +2940,16 @@
     
-%09%09value=value;%0A
         
 %09%09br
@@ -2948,10 +2948,8 @@
     
-%09%09
 brea
@@ -2963,9 +2963,12 @@
     
-%09
+    
 %7D%0A  

remove warning and fix formatting