Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions ha-inject/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/.project
/.classpath
/.settings/
/target/
/bulldozer-temp.json
/.factorypath
16 changes: 16 additions & 0 deletions ha-inject/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version='1.0' encoding='UTF-8'?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<artifactId>ha-inject</artifactId>

<parent>
<groupId>com.g2forge.habitat</groupId>
<artifactId>ha-project</artifactId>
<version>0.0.8-SNAPSHOT</version>
<relativePath>../ha-project/pom.xml</relativePath>
</parent>

<name>Habitat Inject</name>
<description>An API for value injection.</description>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.g2forge.habitat.inject;

public interface IInjectedValue<T> {
public InjectedValueDescriptor<T> getDescriptor();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.g2forge.habitat.inject;

import com.g2forge.alexandria.java.type.ref.ITypeRef;

import lombok.Builder;
import lombok.Data;
import lombok.RequiredArgsConstructor;

@Data
@Builder(toBuilder = true)
@RequiredArgsConstructor
public class InjectedValueDescriptor<T> {
protected final String id;

protected final ITypeRef<T> type;

protected final T fallback;

public InjectedValueDescriptor(Class<?> owner, String name, ITypeRef<T> type, T fallback) {
this(owner.getName() + "." + name, type, fallback);
}

public InjectedValueDescriptor(Class<T> type, T fallback) {
this(type.getName(), ITypeRef.of(type), fallback);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.g2forge.habitat.inject;

import java.util.Map;

import lombok.Builder;
import lombok.Data;
import lombok.RequiredArgsConstructor;

@Data
@Builder(toBuilder = true)
@RequiredArgsConstructor
public class MapInjectedValue<T> implements IInjectedValue<T> {
protected final InjectedValueDescriptor<T> descriptor;

public T get(Map<?, ?> map) {
if ((map != null) && map.containsKey(getDescriptor().getId())) return getDescriptor().getType().cast(map.get(getDescriptor().getId()));
return getDescriptor().getFallback();
}

public Map<? super String, ? super T> inject(Map<? super String, ? super T> map, T value) {
map.put(getDescriptor().getId(), value);
return map;
}
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@
<module>ha-trace</module>
<module>ha-metadata</module>
<module>ha-plugin</module>
<module>ha-inject</module>
</modules>
</project>
Loading