Skip to content

Commit

Permalink
JAMES-1595 Mappings is now a list of Mapping obj, start using it
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/james/project/trunk@1711983 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
mbaechler committed Nov 2, 2015
1 parent feca148 commit 73876d2
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 16 deletions.
@@ -0,0 +1,28 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/

package org.apache.james.rrt.lib;


public interface Mapping {

String asString();

}
Expand Up @@ -21,7 +21,7 @@
package org.apache.james.rrt.lib;


public interface Mappings {
public interface Mappings extends Iterable<Mapping> {

boolean contains(String mapping);

Expand Down
@@ -0,0 +1,57 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/

package org.apache.james.rrt.lib;

import com.google.common.base.Objects;


public class MappingImpl implements Mapping {

public static MappingImpl of(String mapping) {
return new MappingImpl(mapping);
}

private final String mapping;

public MappingImpl(String mapping) {
this.mapping = mapping;
}

@Override
public String asString() {
return mapping;
}

@Override
public boolean equals(Object other) {
if (other instanceof MappingImpl) {
MappingImpl otherMapping = (MappingImpl) other;
return Objects.equal(mapping, otherMapping.mapping);
}
return false;
}

@Override
public int hashCode() {
return Objects.hashCode(mapping);
}

}
Expand Up @@ -25,17 +25,19 @@
import java.util.Iterator;
import java.util.StringTokenizer;

import com.google.common.base.Function;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;

public class MappingsImpl implements Mappings {

public static MappingsImpl empty() {
return new MappingsImpl(new ArrayList<String>());
return builder().build();
}

public static MappingsImpl fromRawString(String raw) {
return new MappingsImpl(mappingToCollection(raw));
return fromCollection(mappingToCollection(raw));
}

private static ArrayList<String> mappingToCollection(String rawMapping) {
Expand All @@ -48,8 +50,12 @@ private static ArrayList<String> mappingToCollection(String rawMapping) {
return map;
}

public static Mappings fromCollection(Collection<String> mappings) {
return new MappingsImpl(mappings);
public static MappingsImpl fromCollection(Collection<String> mappings) {
Builder builder = builder();
for (String mapping: mappings) {
builder.add(mapping);
}
return builder.build();
}

public static Builder from(Mappings from) {
Expand All @@ -64,37 +70,42 @@ public static Builder builder() {

public static class Builder {

private final ImmutableList.Builder<String> mappings;
private final ImmutableList.Builder<Mapping> mappings;

private Builder() {
mappings = ImmutableList.builder();
}

public Builder add(String mapping) {
mappings.add(mapping);
mappings.add(MappingImpl.of(mapping));
return this;
}

public Builder addAll(Mappings mappings) {
this.mappings.addAll(mappings.asStrings());
this.mappings.addAll(mappings);
return this;
}

public Mappings build() {
public MappingsImpl build() {
return new MappingsImpl(mappings.build());
}

}

private final ImmutableList<String> mappings;
private final ImmutableList<Mapping> mappings;

private MappingsImpl(Collection<String> mappings) {
private MappingsImpl(Collection<Mapping> mappings) {
this.mappings = ImmutableList.copyOf(mappings);
}

@Override
public Iterable<String> asStrings() {
return mappings;
return FluentIterable.from(mappings).transform(new Function<Mapping, String>() {
@Override
public String apply(Mapping input) {
return input.asString();
}
});
}

@Override
Expand All @@ -108,11 +119,12 @@ public int size() {
}

@Override
public Mappings remove(String mapping) {
public Mappings remove(String mappingAsString) {
MappingImpl mapping = MappingImpl.of(mappingAsString);
if (mappings.contains(mapping)) {
ArrayList<String> updatedMappings = Lists.newArrayList(mappings);
ArrayList<Mapping> updatedMappings = Lists.newArrayList(mappings);
updatedMappings.remove(mapping);
return MappingsImpl.fromCollection(updatedMappings);
return new MappingsImpl(updatedMappings);
}
return this;
}
Expand All @@ -121,5 +133,10 @@ public Mappings remove(String mapping) {
public boolean isEmpty() {
return mappings.isEmpty();
}

@Override
public Iterator<Mapping> iterator() {
return mappings.iterator();
}

}
Expand Up @@ -112,7 +112,7 @@ public void assertMappingsIsEmpty() throws Throwable {

@Then("mappings for user \"([^\"]*)\" at domain \"([^\"]*)\" should be empty")
public void assertMappingsIsEmpty(String user, String domain) throws Throwable {
assertThat(rewriteTable.getMappings(user, domain).asStrings()).isNullOrEmpty();
assertThat(rewriteTable.getMappings(user, domain)).isNullOrEmpty();
}

@Then("mappings for user \"([^\"]*)\" at domain \"([^\"]*)\" should contains only \"([^\"]*)\"")
Expand Down

0 comments on commit 73876d2

Please sign in to comment.