// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.api.integration.dns;


import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;

/**
 * An in-memory name service for testing purposes.
 *
 * @author mpolden
 */
public class MemoryNameService implements NameService {

    private final Set<Record> records = new TreeSet<>();

    public Set<Record> records() {
        return Collections.unmodifiableSet(records);
    }

    @Override
    public Record createCname(RecordName name, RecordData canonicalName) {
        Record record = new Record(Record.Type.CNAME, name, canonicalName);
        records.add(record);
        return record;
    }

    @Override
    public List<Record> createAlias(RecordName name, Set<AliasTarget> targets) {
        List<Record> records = targets.stream()
                                     .sorted((a, b) -> Comparator.comparing(AliasTarget::name).compare(a, b))
                                     .map(target -> new Record(Record.Type.ALIAS, name,
                                                               RecordData.fqdn(target.name().value())))
                                     .collect(Collectors.toList());
        // Satisfy idempotency contract of interface
        removeRecords(findRecords(Record.Type.ALIAS, name));
        this.records.addAll(records);
        return records;
    }

    @Override
    public List<Record> createTxtRecords(RecordName name, List<RecordData> txtData) {
        List<Record> records = txtData.stream()
                .map(data -> new Record(Record.Type.TXT, name, data))
                .collect(Collectors.toList());
        this.records.addAll(records);
        return records;
    }

    @Override
    public List<Record> findRecords(Record.Type type, RecordName name) {
        return records.stream()
                      .filter(record -> record.type() == type && record.name().equals(name))
                      .collect(Collectors.toUnmodifiableList());
    }

    @Override
    public List<Record> findRecords(Record.Type type, RecordData data) {
        return records.stream()
                      .filter(record -> record.type() == type && record.data().equals(data))
                      .collect(Collectors.toUnmodifiableList());
    }

    @Override
    public void updateRecord(Record record, RecordData newData) {
        List<Record> records = findRecords(record.type(), record.name());
        if (records.isEmpty()) {
            throw new IllegalArgumentException("No record with data '" + newData.asString() + "' exists");
        }
        if (records.size() > 1) {
            throw new IllegalArgumentException("Cannot update multi-value record '" + record.name().asString() +
                                               "' with '" + newData.asString() + "'");
        }
        Record existing = records.get(0);
        this.records.remove(existing);
        this.records.add(new Record(existing.type(), existing.name(), newData));
    }

    @Override
    public void removeRecords(List<Record> records) {
        this.records.removeAll(records);
    }

}

@@ -732,22 +732,19 @@
         
-Record
+var
  record 
@@ -955,36 +955,27 @@
 ) %7B%0A        
-List%3CRecord%3E
+var
  records = t
@@ -986,32 +986,24 @@
 ts.stream()%0A
-        
             
@@ -1117,32 +1117,24 @@
             
-  
-      
 .map(target 
@@ -1227,24 +1227,16 @@
         
-        
 RecordDa
@@ -1268,24 +1268,16 @@
 ue())))%0A
-        
         
@@ -1615,36 +1615,27 @@
 ) %7B%0A        
-List%3CRecord%3E
+var
  records = t
@@ -1642,32 +1642,45 @@
 xtData.stream()%0A
+             
                 
@@ -1729,24 +1729,37 @@
 ame, data))%0A
+             
             
@@ -2518,28 +2518,19 @@
         
-List%3CRecord%3E
+var
  records
@@ -2977,22 +2977,19 @@
         
-Record
+var
  existin

Simplify MemoryNameService