Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /* *********************************************************************
   * This Source Code Form is subject to the terms of the Mozilla Public
   * License, v. 2.0.
   * 
   * If a copy of the MPL was not distributed with this file, You can obtain
   * one at http://mozilla.org/MPL/2.0/.
   * 
   * This Source Code Form is "Incompatible With Secondary Licenses", as
   * defined by the Mozilla Public License, v. 2.0.
  * ********************************************************************* */
 package fiftyone.mobile.detection.matchers.editdistance;
 
 import java.util.List;
Class that holds the Edit Distance handler method.

Author(s):
51Degrees.mobi
Version:
2.2.8.9
 
 public class Matcher extends fiftyone.mobile.detection.matchers.Matcher {

    
Holds the method to service Edit Distance Handler requests.
 
     static class ServiceRequest implements Runnable {
        
The request Object
 
         private Request _request;
 
         public ServiceRequest(final Request request) {
              = request;
         }

        
Algorithm used to carry out a Edit Distance Handler check.
 
         @Override
         public void run() {
             final String userAgent = .getUserAgent();
             final int[][] rows = new int[userAgent.length() + 1][userAgent.length() + 1];
             BaseDeviceInfo current = .next();
             while (current != null) {
                 // Perform the edit distance check.
                 final int distance = Algorithms.EditDistance(
                         rows,
                         userAgent,
                         current.getUserAgent(),
                         .getResults().);
 
                 // Store the results.
                 if (distance <= .getResults().) {
                     synchronized (.getResults()) {
                         if (distance < .getResults().) {
                             .getResults(). = distance;
                             .getResults().clear();
                             .getResults().add(
                                     current,
                                     .getHandler(),
                                     distance,
                                     userAgent);
                         } else if (distance == .getResults().) {
                             .getResults().add(
                                     current,
                                     .getHandler(),
                                     distance,
                                     userAgent);
                         }
                     }
                 }
                 current = .next();
             }
         }
     }
    
    
Creates a _logger for this class
 
     static final Logger _logger = LoggerFactory.getLogger(Matcher.class);

    
Routine for matching when only one processor is being used.

Parameters:
userAgent User Agent being matched.
handler The handler associated with the matching request.
Returns:
The best devices matching the userAgent.
    public static Results matchSingleProcessor(
            final String userAgent
            final Handler handler) {
        return matchSingleProcessor(new Request(userAgenthandler));
    }
    
    
Routine for matching when only one processor is being used.

Parameters:
request configured with userAgent and handler
Returns:
The best devices matching the userAgent.
    private static Results matchSingleProcessor(final Request request) {
        // Process the request.
        new ServiceRequest(request).run();
        // Return the results.
        return request.getResults();
    }

    
Routine for matching when multiple processors are being used.

Parameters:
userAgent User Agent being matched.
handler The handler associated with the matching request.
threadPool The Provider Object's thread pool.
Returns:
The best devices matching the userAgent.
Throws:
java.lang.InterruptedException
    public static Results matchMultiProcessor(
            final String userAgent
            final Handler handler
            final ThreadPoolExecutor threadPoolthrows InterruptedException {
        // Create the request.
        final Request request = new Request(userAgenthandler);
        if (request.getCount() > 0) {
            try {
                int poolSize = Matcher.getThreadCount(threadPool);
                
                // Create a task for each processor, submitting the task for 
                // execution.
                final List<Future<Object>> futures = 
                        new ArrayList<Future<Object>>(poolSize);
                
                final Callable<Objectc = 
                        Executors.callable(new ServiceRequest(request));
                
                
                for (int i = 0; i < poolSizei++) {
                    futures.add(threadPool.submit(c));
                }
                // Wait for all the tasks to finish executing before returning 
                // the results.
                for(Future<Objectfuture : futures) {
                    try {
                        future.get();
                    } catch (ExecutionException ex) {
                        .warn(
                        "Multi thread edit distance matcher execution exception.",
                        ex);
                    }
                }
            }
            catch (RejectedExecutionException ex) {
                .warn(
                    "Multi thread edit distance matcher execution rejected. " +
                    "Falling back to single threaded matching.",
                    ex);
                return matchSingleProcessor(request);
            }
        }
        // Return the results.
        return request.getResults();
    }
New to GrepCode? Check out our FAQ X