2014-05-12 23 views
0

제네릭 형식을 사용하여 전략 구성 요소의 구체적인 구현으로 약간의 문제가 발생했습니다. 사람이 예와 올바른 방향으로 날 지점 수 있다면전략 구성 요소의 제네릭 및 구체적인 구현

궁금하십니까?

여기는 내가으로 작업하고 있지만, ArrayList를 만들 때 Decode 메서드를 호출 할 때 Decode 메서드를 선언 할 때마다 잡히다. 놀랍지도 않습니다. 여기

public class CsvFormat<T,T1> implements FormatStrategy<T,T1> { 


public CsvFormat(boolean header) { 
    setHeader(header); 

@Override 
public final T decode(T1 csvData) { 
    csvData = new ArrayList(); //ERROR**** 
    List<Map<String, String>> decodedData = new ArrayList<Map<String, String>>(); //turn collection into an array of maps 
    if (this.hasHeader()) { 
     decodeDataWithHeader(csvData, decodedData); 
    } else { 
     decodeDataNoHeader(csvData, decodedData); 
    } 
    return decodedData; 
} 

private void decodeDataNoHeader(List<String> csvData, List<Map<String, String>> records) { 
    int recordCount = FIRST_IDX; 
    List<String> fields = null; //= Arrays.asList(csvData.get(recordCount).split(DELIM)); //turn line into a list, first record 
    for (String data : csvData) { //for each unformatted string 
     int delimIndex = FIRST_IDX; //reset delim 
     fields = Arrays.asList(data.split(DELIM));//after header, start mapping 
     records.add(new LinkedHashMap<String, String>()); //make a new map 
     recordCount++; 
     for (String field : fields) { 
      final String KEY_ID = "Column-" + (delimIndex + RECORD_BUFFER); 
      records.get(records.size() - RECORD_BUFFER).put(KEY_ID, field); 
      delimIndex++; 
     } 
    } 
} 

내가 ..

로 지금까지 생각할 수있는 오류들이 전달되는 어떤 객체를 기반으로 디코드 메소드를 오버로드하는 것입니다 않고 위 달성하는 유일한 방법을 시작했다 무엇인가
public class CsvFormat implements FormatStrategy< 
List<Map<String, String>>, List<String>> { 


public CsvFormat(boolean header) { 
    setHeader(header); 
} 


@Override 
public final List<Map<String, String>> decode(List<String> csvData) { 
    List<Map<String, String>> decodedData = new ArrayList<Map<String, String>>(); //turn collection into an array of maps 
    if (this.hasHeader()) { 
     decodeDataWithHeader(csvData, decodedData); 
    } else { 
     decodeDataNoHeader(csvData, decodedData); 
    } 
    return decodedData; 
} 

private void decodeDataNoHeader(List<String> csvData, List<Map<String, String>> records) { 
    int recordCount = FIRST_IDX; 
    List<String> fields = null; //= Arrays.asList(csvData.get(recordCount).split(DELIM)); //turn line into a list, first record 
    for (String data : csvData) { //for each unformatted string 
     int delimIndex = FIRST_IDX; //reset delim 
     fields = Arrays.asList(data.split(DELIM));//after header, start mapping 
     records.add(new LinkedHashMap<String, String>()); //make a new map 
     recordCount++; 
     for (String field : fields) { 
      final String KEY_ID = "Column-" + (delimIndex + RECORD_BUFFER); 
      records.get(records.size() - RECORD_BUFFER).put(KEY_ID, field); 
      delimIndex++; 
     } 
    } 
} 

답변

1

사실 당신이 "시작"의 예는 정확히 잘 보인다. 당신은 입력으로 List<String> 필요로하는 decode 방법을 쓴, 그래서 당신이 T1으로 특정 유형과 FormatStrategy 인터페이스를 구현하는 것 같은 출력 유형 T 간다 점을 추론하기 위하여 서있다.

왜 당신은 당신이 실제로 패턴을 따라 당신이 걱정하는 각각의 특정 구체적인 유형에 대한 새로운 클래스를 만들 수있는 입력 및 안전하지 않은 캐스팅의 부하의 구역질 런타임 검사를 할 것인가?

+0

좋습니다. 따라서 FormatStrategy의보다 구체적인 클래스를 구현하고 그런 식으로 작업 할 것입니다. 감사합니다. 도망 가기 전에 물어 봤기 때문에 기쁘다. 코드를 엉망으로 만들기 시작했다. 나는이 일반 유형으로 얼마나 멀리 달릴 수 있는지 알지 못했지만 지금은 그렇게하고 있습니다. 고맙습니다. – nckbrz

1

데이터를 디코딩하기 위해 작성한 코드는 항상 List<Map<String, String>>을 반환하며 List<String> 만 입력 할 수 있으므로 클래스에 유형 매개 변수가있는 이유가 없습니다. 그래서 네가 시작한 것은 맞을 것 같은데, 왜 네가 그것에 만족하지 않는거야?

+0

디코딩 할지도 목록을 항상 전달하고 싶지 않으므로 다른 방법으로 FormatStrategy를 구현하는 별도의 개체를 만드는 것이 가장 좋습니다. 답변 감사합니다. – nckbrz