2016-09-13 8 views
6

मैं समतल जावा को स्कैला में बदलकर समझने की कोशिश कर रहा हूं, ताकि मुझे बेहतर समझ मिल सके।जावा 8 मानचित्र, फ़िल्टर और स्ट्रीम को स्कैला में कैसे परिवर्तित करें?

जावा 8 मानचित्र, फ़िल्टर और स्ट्रीम को स्कैला कैसे परिवर्तित करें?

public Set<String> getValidUsages(String itemId, long sNo, Date timeOfAccess) {              
    Set<String> itemSet = Sets.newHashSet();                         
    TestWindows testWindows = items.get(itemId).getTestWindows();               

    final boolean isTV = existsEligibleTestWindow(testWindows.getTV(), timeOfAccess);          
    if (isTV) {                                
     itemSet.add(TV);                            
    } else {                                  
     final boolean isCableUseable = existsEligibleTestWindow(testWindows.getCableUse(), timeOfAccess);       
     final boolean isWifi = existsEligibleTestWindow(testWindows.getWifi(), timeOfAccess);        
     if (isCableUseable || isWifi) {                          
      itemSet.add(MOVIE);                           
     }                                  
    }                                   

    if (testWindows.getUsageIds() != null) {                       
     itemSet.addAll(testWindows.getUsageIds()                      
       .entrySet()                              
       .stream()                              
       .filter(entry -> existsEligibleTestWindow(entry.getValue(), timeOfAccess))            
       .map(Map.Entry::getKey)                           
       .collect(Collectors.toSet()));                         
    }                                   

    return itemSet;                                
}                                    

private boolean existsEligibleTestWindow(List<TestWindow> windows, Date timeOfAccess) {           
    if (windows != null) {                              
     return windows.stream()                             
       .filter(w -> withinDateRange(timeOfAccess, w))                     
       .findAny()                              
       .isPresent();                             
    }                                   
    return false;                                
}                                    

private boolean withinDateRange(Date toCheck, TestWindow window) {                  
    return toCheck.after(window.getStartTime()) && toCheck.before(window.getEndTime());               
} 

मैंने कोशिश की:

मैं निम्नलिखित जावा 8 कोड जो मैं स्काला कन्वर्ट करने के लिए कोशिश कर रहा हूँ है

def withinDateRange(toCheck: Date, window: TestWindow): Boolean = { 
    toCheck.after(window.getStartTime) && toCheck.before(window.getEndTime) 
    } 


    def getValidUsages(itemId: String, sNo: Long, timeOfAccess: Date): Set[String] = { 
    var itemSet = Sets.newHashSet() 
    val testWindows = items.value(itemId).getTestWindows 
    val isTV = existsEligibleTestWindow(testWindows.get(0).getTV, timeOfAccess) 
    if (isTV) { 
     itemSet += TV 
    } else { 
     val isCableUseable = existsEligibleTestWindow(testWindows.get(0).getCableUse, timeOfAccess) 
     val isWifi = existsEligibleTestWindow(testWindows.get(0).getWifi, timeOfAccess) 
     if (isCableUseable || isWifi) { 
     itemSet += MOVIE 
     } 
    } 
    if (testWindows.get(0).getUsageIds != null) { 
     itemSet.addAll(testWindows.get(0).getUsageIds.entrySet().stream() 
     .filter((x) => existsEligibleTestWindow(x._2, timeOfAccess)).map(x => Map.Entry._1) 
     .collect(Collectors.toSet())) 
    } 
    itemSet 
    } 


    def existsEligibleConsumptionWindow(windows: List[ConsumptionWindow], timeOfAccess: Date): Boolean = { 
if (windows != null) { 
    return windows.exists((x) => withinDateRange(timeOfAccess, x)) 
} 
false 
} 

लेकिन जब फिल्टर और धारा कर त्रुटि मिल रही है। क्या कोई दिशा निर्देशित कर सकता है? कोई संदर्भ? मैं getValidUsages पर त्रुटि हो रही है:

compile error “cannot resolve reference project with such signature 
+0

त्रुटि संदेश क्या है? – Nyavro

+0

जावा सिंटैक्स को स्कैला सिंटैक्स में बदलने के बजाय, क्या आपने जावाडोक की जांच करने की कोशिश की है? आपको गैर आलसी धारा की आवश्यकता नहीं है। 'फ़िल्टर', 'मानचित्र', 'isEmpty'' सूची ',' सेट' पर सीधे 'फ़िल्टर' के बारे में कैसे? – waltersu

+2

यदि आप जावा संग्रह का उपयोग करना जारी रखना चाहते हैं, तो आपको शायद इसकी आवश्यकता होगी: https://github.com/scala/scala-java8-compat#using-java-8-streams-with-scala-function-converters –

उत्तर

-1
def getValidUsages(itemId: String, sNo: long, timeOfAcess: Date): Set[String] = { 
    var itemSet: Set[String] = Sets.newHashSet() 
    val testWindows: TestWindows = items.get(itemId).getTestWindows() 
    val isTV: Boolean = existsEligibleTestWindow(testWindows.getTV(), timeOfAccess) 

    isTV match { 
    case true => itemSet.add(TV) 
    case false => { 
     val isCableUseable: Boolean = existsEligibleTestWindow(testWindows.getCableUse(), timeOfAcess) 
     val isWifi: Boolean = existsEligibleTestWindow(testWindows.getWifi(), timeOfAccess) 
     if(isCableUseable || isWifi) { 
     itemSet.add(MOVIE) 
     } 
    } 
    } 

    if(testWindows.getUsageIds() != null) { 
    itemSet.addAll(testWindows.getUsageIds() 
       .stream. 
       .filter(entry => existsEligibleTestWindow(entry._2, timeOfAccess)) 
       .map(filteredData => Map.Entry._1) 
       .collect Collectors.toSet()) 
    } 
    itemSet 
} 

def existsEligibleTestWindow(windows: List[TestWindow], timeOfAcess: Date): Boolean = { 
    windows match { 
    case null => false 
    case _ => windows.stream.filter(data => withinDateRange(timeOfAcess), data).findAny().isPresent 
    } 
} 

गुड लक :)

+0

यह अभी भी मुझे देता है त्रुटि – Swetha

+0

क्या आप त्रुटि पोस्ट कर सकते हैं? – HuntsMan

0

यह जवाब देने के लिए के बाद से मैं प्रकार आप उपयोग से अपरिचित हूँ कुछ हद तक मुश्किल है। लेकिन अगर मुझे लगता है कि वहाँ की तरह प्रकार के होते हैं निम्नलिखित:

trait Window { 
    def getStartTime: LocalDate 
    def getEndTime: LocalDate 
} 
trait TestWindows extends Window { 
    def getTV: List[Window] 
    def getCableUse: List[Window] 
    def getWifi: List[Window] 
    def getUsageIds: Map[String, List[Window]] 
} 

तो आप ऐसा कर सकते हैं:

def withinDateRange(toCheck: LocalDate)(window: Window): Boolean = 
    window.getStartTime.isBefore(toCheck) && window.getEndTime.isAfter(toCheck) 

    // Nothing should ever be null in Scala. If it's possible you don't have any ConsumptionWindows you should either 
    // model it as an empty list or an Option[List[ConsumptionWindow]] 
    def existsEligibleTestWindow(windows: List[Window], 
           timeOfAccess: LocalDate): Boolean = 
    windows.exists(withinDateRange(timeOfAccess)) 


    def getValidUsages(testWindows: TestWindows, timeOfAccess: LocalDate): Set[String] = { 
    val isTV = existsEligibleTestWindow(testWindows.getTV, timeOfAccess) 
    val isCableUse = existsEligibleTestWindow(testWindows.getCableUse, timeOfAccess) 
    val isWifi = existsEligibleTestWindow(testWindows.getWifi, timeOfAccess) 
    val tvOrMovie: Option[String] = if (isTV) Some("TV") 
            else if (isCableUse || isWifi) Some("MOVIE") 
            else None 

    val byUsageId = testWindows.getUsageIds.collect { case (key, windows) if existsEligibleTestWindow(windows, timeOfAccess) => key }.toSet 

    tvOrMovie.toSet ++ byUsageId 
    } 

अपने मूल कोड में नहीं था शायद कुछ items मूल्य है, लेकिन इसके बाद के संस्करण में मैं सिर्फ मान getValidUsages फ़ंक्शन के बाहर आप TestWindows testWindows = items.get(itemId).getTestWindows() पर काम करते हैं।

मेरा उदाहरण जावा संरचनाओं का बिल्कुल उपयोग नहीं करता है और केवल स्कैला कोर संग्रह का उपयोग करता है। दूसरा मुख्य अंतर यह है कि मैं अपरिवर्तनीय डेटा संरचनाओं का उपयोग करता हूं, जो मुझे लगता है, थोड़ा आसान और आमतौर पर सुरक्षित है।

नोट के कुछ आइटम: 1) विकल्प .toSet ऑपरेशन का परिणाम एक खाली सेट में होता है जब किसी को भी नहीं कहा जाता है। 2) withinDateRange विधि के लिए फ़ंक्शन करी का उपयोग करने का एक उदाहरण है। 3) मुझे स्पष्ट रूप से पता नहीं है कि आपके मूल प्रकार क्या करते हैं और प्रासंगिक भागों में अनुमान लगाना था।

0

समस्या यह प्रतीत होती है कि स्कैला के मानचित्र & फ़िल्टर संचालन के आधार पर आप स्कैला में जावा प्रकार का उपयोग कर रहे हैं। इसकी अपनी परेशानियां हैं लेकिन यदि आप सूची/संग्रह को Scala's collections पर पहले कनवर्ट करते हैं (चेतावनी, स्कैला प्रकार डिफ़ॉल्ट रूप से अपरिवर्तनीय हैं), तो आप जावा के stream() विधि को कॉल किए बिना मानचित्र/फ़िल्टर संचालन का उपयोग करने में सक्षम होना चाहिए।

संबंधित मुद्दे