package bluej.stride.framedjava.errors;

import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import bluej.utility.Utility;
import bluej.utility.javafx.FXPlatformConsumer;
import threadchecker.OnThread;
import threadchecker.Tag;

class Correction
extends FixSuggestion{    
   private final String correction;
   
   private final String display;
   
   private final FXPlatformConsumer<String> replacer;

   
   
   
   @OnThread(Tag.Any)
   
   private Correction(String correction, FXPlatformConsumer<String> replacer, String display)
   {        
      this.correction = correction;
       
      this.display = display;
       
      this.replacer = replacer;     
      }

   
   @Override
   
   public String getDescription()
   {        
      return "Correct to: " + display;     
      }

   
   @Override
   @OnThread(Tag.FXPlatform)
   
   public void execute()
   {
      replacer.accept(correction);     
      }
    
   
   private final static int MAX_EDIT_DISTANCE = 2;

   @OnThread(Tag.Any)
   
   private static class StringAndDist
{              
public final CorrectionInfo value;
       
      public final int distance;
        
       
      public StringAndDist(CorrectionInfo value, int distance)
       {            
         this.value = value;
           
         this.distance = distance;         
         }     
      }

   @OnThread(Tag.Any)
   
   public static interface CorrectionInfo
{              

      public String getCorrection();
       
      
      public String getDisplay();     
      }

   @OnThread(Tag.Any)
   
   public static class SimpleCorrectionInfo
implements CorrectionInfo
   {        
      private String correction;
       
      public SimpleCorrectionInfo(String correction)
       {            
         this.correction = correction;         
         }
       
      public String getCorrection()
{
return correction; 
         }
       
      public String getDisplay()
{
return correction; 
         }     
      }

   
   
   @OnThread(Tag.Any)
   
   public static List winnowAndCreateCorrections(String cur, Stream<CorrectionInfo> possibleCorrections, FXPlatformConsumer<String> replacer)
   {        
      return possibleCorrections
               
      .map(n -> new StringAndDist(n, Utility.editDistance(cur.toLowerCase(), n.getCorrection().toLowerCase())))
      .filter(sd -> sd.distance <= MAX_EDIT_DISTANCE)
      .sorted((a, b) -> Integer.compare(a.distance, b.distance))
      .limit(3)
               
      .map(sd -> new Correction(sd.value.getCorrection(), replacer, sd.value.getDisplay()))
      .collect(Collectors.toList());     
      }       
   }

.   - Correction
.   Correction
.   getDescription
.   execute

top, use, map, class Correction . StringAndDist

.   StringAndDist

top, use, map, interface Correction . StringAndDist . CorrectionInfo

.   getCorrection
.   getDisplay

top, use, map, class Correction . StringAndDist . CorrectionInfo . SimpleCorrectionInfo

.   SimpleCorrectionInfo
.   getCorrection
.   getDisplay
.   winnowAndCreateCorrections




115 neLoCode + 0 LoComm