/*
 * Licensed 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 com.facebook.presto.hive.metastore;

import io.airlift.stats.CounterStat;
import io.airlift.stats.TimeStat;
import org.apache.hadoop.hive.metastore.api.MetaException;
import org.apache.thrift.TException;
import org.weakref.jmx.Managed;
import org.weakref.jmx.Nested;

import javax.annotation.concurrent.ThreadSafe;

import java.util.concurrent.Callable;

@ThreadSafe
public class HiveMetastoreApiStats
{
    private final TimeStat time = new TimeStat();
    private final CounterStat totalFailures = new CounterStat();
    private final CounterStat metastoreExceptions = new CounterStat();
    private final CounterStat thriftExceptions = new CounterStat();

    public <V> Callable<V> wrap(final Callable<V> callable)
    {
        return new Callable<V>()
        {
            @Override
            public V call()
                    throws Exception
            {
                try (TimeStat.BlockTimer timer = time.time()) {
                    return callable.call();
                }
                catch (Exception e) {
                    if (e instanceof MetaException) {
                        metastoreExceptions.update(1);
                        // Need to throw here instead of falling through due to JDK-8059299
                        totalFailures.update(1);
                        throw e;
                    }
                    else if (e instanceof TException) {
                        thriftExceptions.update(1);
                        // Need to throw here instead of falling through due to JDK-8059299
                        totalFailures.update(1);
                        throw e;
                    }
                    totalFailures.update(1);

                    throw e;
                }
            }
        };
    }

    @Managed
    @Nested
    public TimeStat getTime()
    {
        return time;
    }

    @Managed
    @Nested
    public CounterStat getTotalFailures()
    {
        return totalFailures;
    }

    @Managed
    @Nested
    public CounterStat getThriftExceptions()
    {
        return thriftExceptions;
    }

    @Managed
    @Nested
    public CounterStat getMetastoreExceptions()
    {
        return metastoreExceptions;
    }
}

@@ -1254,22 +1254,16 @@
 V%3E wrap(
-final 
 Callable
@@ -1301,141 +1301,16 @@
 urn 
-new Callable%3CV%3E()%0A        %7B%0A            @Override%0A            public V call()%0A                    throws Exception%0A            %7B%0A    
+() -%3E %7B%0A
     
@@ -1346,13 +1346,15 @@
 mer 
-timer
+ignored
  = t
@@ -1363,28 +1363,24 @@
 e.time()) %7B%0A
-    
             
@@ -1407,20 +1407,16 @@
 call();%0A
-    
         
@@ -1433,20 +1433,16 @@
         
-    
 catch (E
@@ -1451,28 +1451,24 @@
 eption e) %7B%0A
-    
             
@@ -1525,20 +1525,16 @@
         
-  
-  
 metastor
@@ -1548,36 +1548,32 @@
 ions.update(1);%0A
-    
                 
@@ -1656,36 +1656,32 @@
                 
-   
- 
 totalFailures.up
@@ -1681,36 +1681,32 @@
 ures.update(1);%0A
-    
                 
@@ -1726,37 +1726,34 @@
                 
-    
 %7D
+%0A
 %0A               
@@ -1757,17 +1757,8 @@
     
-    else 
 if (
@@ -1804,20 +1804,16 @@
         
-    
 thriftEx
@@ -1824,36 +1824,32 @@
 ions.update(1);%0A
-    
                 
@@ -1924,112 +1924,8 @@
 299%0A
-                        totalFailures.update(1);%0A                        throw e;%0A                    %7D%0A
     
@@ -1965,17 +1965,16 @@
 ate(1);%0A
-%0A
         
@@ -2003,32 +2003,99 @@
                %7D
+%0A%0A                totalFailures.update(1);%0A                throw e;
 %0A            %7D%0A 

Migrate HiveMetastoreApiStats to use lambdas