/*
 * Copyright (c) 2015 Google Inc.
 *
 * 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.google.cloud.bigquery.samples;

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.bigquery.Bigquery;
import com.google.api.services.bigquery.BigqueryScopes;

import java.io.IOException;
import java.util.Collection;

public class BigqueryServiceFactory {

  private static Bigquery service = null;
  private static Object service_lock = new Object();

  public static Bigquery getService() throws IOException{
    if(service==null){
      synchronized(service_lock){
        if(service==null){
          service=createAuthorizedClient();
        }
      }
    }
    return service;
  }

  // [START get_service]
  private static Bigquery createAuthorizedClient() throws IOException {
    Collection<String> BIGQUERY_SCOPES = BigqueryScopes.all();
    HttpTransport TRANSPORT = new NetHttpTransport();
    JsonFactory JSON_FACTORY = new JacksonFactory();
    GoogleCredential credential = GoogleCredential.getApplicationDefault(TRANSPORT, JSON_FACTORY);
    if(credential.createScopedRequired()){
      credential = credential.createScoped(BIGQUERY_SCOPES);
    }
    return new Bigquery.Builder(TRANSPORT, JSON_FACTORY, credential).setApplicationName("BigQuery Samples").build();
  }
  // [END get_service]

}

@@ -111,13 +111,17 @@
  may
+%0A *
  not 
+ 
 use 
@@ -136,19 +136,16 @@
 e except
-%0A *
  in comp
@@ -185,16 +185,19 @@
 obtain a
+%0A *
  copy of
@@ -334,16 +334,19 @@
 software
+%0A *
  distrib
@@ -367,19 +367,16 @@
  License
-%0A *
  is dist
@@ -411,16 +411,19 @@
  WITHOUT
+%0A *
  WARRANT
@@ -467,19 +467,16 @@
  express
-%0A *
  or impl
@@ -483,24 +483,27 @@
 ied. See the
+%0A *
  License for
@@ -1088,88 +1088,443 @@
 n;%0A%0A
-public class BigqueryServiceFactory %7B%0A%0A  private static Bigquery service = null;
+/**%0A * This class creates our Service to connect to Bigquery including auth.%0A */%0Apublic final class BigqueryServiceFactory %7B%0A%0A  /**%0A   * Private constructor to disable creation of this utility Factory class.%0A   */%0A  private BigqueryServiceFactory() %7B%0A%0A  %7D%0A%0A  /**%0A   * Singleton service used through the app.%0A   */%0A  private static Bigquery service = null;%0A%0A  /**%0A   * Mutex created to create the singleton in thread-safe fashion.%0A   */
 %0A  p
@@ -1551,18 +1551,17 @@
  service
-_l
+L
 ock = ne
@@ -1573,16 +1573,209 @@
 ect();%0A%0A
+  /**%0A   * Threadsafe Factory that provides an authorized Bigquery service.%0A   * @return The Bigquery service%0A   * @throws IOException Thronw if there is an error connecting to Bigquery.%0A   */%0A
   public
@@ -1826,16 +1826,17 @@
 tion
+ 
 %7B%0A    if
 (ser
@@ -1827,32 +1827,33 @@
 ion %7B%0A    if
+ 
 (service
 ==null)%7B%0A   
@@ -1832,39 +1832,42 @@
 %0A    if (service
+ 
 ==
+ 
 null)
+ 
 %7B%0A      synchron
@@ -1870,24 +1870,25 @@
 hronized
+ 
 (service
 _lock)%7B%0A
@@ -1883,14 +1883,14 @@
 vice
-_l
+L
 ock)
+ 
 %7B%0A  
@@ -1897,24 +1897,25 @@
       if
+ 
 (service
 ==null)%7B
@@ -1910,15 +1910,18 @@
 vice
+ 
 ==
+ 
 null)
+ 
 %7B%0A  
@@ -1935,17 +1935,19 @@
  service
-=
+ = 
 createAu
@@ -2013,16 +2013,180 @@
 e;%0A  %7D%0A%0A
+  /**%0A   * Creates an authorized client to Google Bigquery.%0A   * @return The BigQuery Service%0A   * @throws IOException Thrown if there is an error connecting%0A   */%0A
   // %5BST
@@ -2297,31 +2297,30 @@
 String%3E 
-BIGQUERY_SCOPES
+bigqueryScopes
  = Bigqu
@@ -2354,25 +2354,25 @@
 ansport 
-TRANSPORT
+transport
  = new N
@@ -2406,28 +2406,27 @@
 Factory 
-JSON_FACTORY
+jsonFactory
  = new J
@@ -2475,16 +2475,17 @@
 ential =
+ 
  GoogleC
@@ -2516,39 +2516,51 @@
 Default(
-TRANSPORT, JSON_FACTORY
+%0A            transport, jsonFactory
 );%0A    i
@@ -2560,16 +2560,17 @@
 ;%0A    if
+ 
 (credent
@@ -2596,16 +2596,17 @@
 uired())
+ 
 %7B%0A      
@@ -2646,23 +2646,22 @@
 ped(
-BIGQUERY_SCOPES
+bigqueryScopes
 );%0A 
@@ -2701,44 +2701,56 @@
 der(
-TRANSPORT, JSON_FACTORY, credential)
+transport, jsonFactory, credential)%0A            
 .set

Fix style in BigqueryServiceFactory