Skip to content
This repository has been archived by the owner on Jan 27, 2025. It is now read-only.

Commit

Permalink
Introduce Mapping abstraction. (#64)
Browse files Browse the repository at this point in the history
Intended as a shared type for records and map values.
  • Loading branch information
blackwinter committed Nov 15, 2021
1 parent e4d8a47 commit 8eb5900
Show file tree
Hide file tree
Showing 4 changed files with 368 additions and 299 deletions.
146 changes: 146 additions & 0 deletions metafix/src/main/java/org/metafacture/metafix/Mapping.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/*
* Copyright 2021 hbz NRW
*
* Licensed 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.metafacture.metafix;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.BiConsumer;

/**
* Represents a mapping of metadata fields and values.
*/
public class Mapping {

private static final String EMPTY = "";

private final Map<String, Object> map = new LinkedHashMap<>();

/**
* Creates an empty instance of {@link Mapping}.
*/
public Mapping() {
}

/**
* Checks whether this mapping contains the metadata field.
*
* @param field the field name
* @return true if this mapping contains the metadata field, false otherwise
*/
public boolean containsField(final String field) {
return map.containsKey(field);
}

/**
* Checks whether this mapping is empty.
*
* @return true if this mapping is empty, false otherwise
*/
public boolean isEmpty() {
return map.isEmpty();
}

/**
* Gets the number of field/value pairs in this mapping.
*
* @return the number of field/value pairs in this mapping
*/
public int size() {
return map.size();
}

/**
* Adds a field/value pair to this mapping, provided it's not {@code null}.
*
* @param field the field name
* @param value the metadata value
*/
public void put(final String field, final Object value) {
if (value != null) {
map.put(field, value);
}
}

/**
* {@link #put(String, Object) Replaces} a field/value pair in this mapping,
* provided the field name is already {@link #containsField(String) present}.
*
* @param field the field name
* @param value the metadata value
*/
public void replace(final String field, final Object value) {
if (containsField(field)) {
put(field, value);
}
}

/**
* Retrieves the field value from this mapping.
*
* @param field the field name
* @return the metadata value
*/
public Object get(final String field) {
return map.get(field);
}

/**
* Removes the given field/value pair from this mapping.
*
* @param field the field name
*/
public void remove(final String field) {
map.remove(field);
}

/**
* Retains only the given field/value pairs in this mapping.
*
* @param fields the field names
*/
public void retainFields(final Collection<String> fields) {
map.keySet().retainAll(fields);
}

/**
* Removes all field/value pairs from this mapping whose value is empty.
*/
public void removeEmptyValues() {
map.values().removeIf(EMPTY::equals);
}

/**
* Iterates over all field/value pairs in this mapping.
*
* @param consumer the action to be performed for each field/value pair
*/
public void forEach(final BiConsumer<String, Object> consumer) {
map.forEach(consumer);
}

@Override
public String toString() {
return map.toString();
}

// TODO: Replace map accesses with record operations!
public Map<String, Object> temporarilyGetMap() {
return map;
}

}
117 changes: 3 additions & 114 deletions metafix/src/main/java/org/metafacture/metafix/Record.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,10 @@

package org.metafacture.metafix;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.BiConsumer;

/**
* Represents a metadata record, i.e., a mapping of fields and values.
* Represents a metadata record, i.e., a {@link Mapping} of fields and values.
*/
public class Record {

private static final String EMPTY = "";

private final Map<String, Object> map = new LinkedHashMap<>();
public class Record extends Mapping {

private boolean reject;

Expand Down Expand Up @@ -70,112 +61,10 @@ public boolean getReject() {
return reject;
}

/**
* Checks whether this record contains the metadata field.
*
* @param field the field name
* @return true if this record contains the metadata field, false otherwise
*/
public boolean containsField(final String field) {
return map.containsKey(field);
}

/**
* Checks whether this record is empty.
*
* @return true if this record is empty, false otherwise
*/
public boolean isEmpty() {
return map.isEmpty();
}

/**
* Gets the number of field/value pairs in this record.
*
* @return the number of field/value pairs in this record
*/
public int size() {
return map.size();
}

/**
* Adds a field/value pair to this record, provided it's not {@code null}.
*
* @param field the field name
* @param value the metadata value
*/
public void put(final String field, final Object value) {
if (value != null) {
map.put(field, value);
}
}

/**
* {@link #put(String, Object) Replaces} a field/value pair in this record,
* provided the field name is already {@link #containsField(String) present}.
*
* @param field the field name
* @param value the metadata value
*/
public void replace(final String field, final Object value) {
if (containsField(field)) {
put(field, value);
}
}

/**
* Retrieves the field value from this record.
*
* @param field the field name
* @return the metadata value
*/
public Object get(final String field) {
return map.get(field);
}

/**
* Removes the given field/value pair from this record.
*
* @param field the field name
*/
public void remove(final String field) {
map.remove(field);
}

/**
* Retains only the given field/value pairs in this record.
*
* @param fields the field names
*/
public void retainFields(final Collection<String> fields) {
map.keySet().retainAll(fields);
}

/**
* Removes all field/value pairs from this record whose value is empty.
*/
public void removeEmptyValues() {
map.values().removeIf(EMPTY::equals);
}

/**
* Iterates over all field/value pairs in this record.
*
* @param consumer the action to be performed for each field/value pair
*/
public void forEach(final BiConsumer<String, Object> consumer) {
map.forEach(consumer);
}

@Override
public String toString() {
// TODO: Improve string representation? Include reject status, etc.?
return map.toString();
}

// TODO: Replace map accesses with record operations!
public Map<String, Object> temporarilyGetMap() {
return map;
return super.toString();
}

}
Loading

0 comments on commit 8eb5900

Please sign in to comment.