Indentations break the feed.
[SquirrelJME.git] / modules / cldc-compact / src / main / java / cc / squirreljme / jvm / suite / ProvidedInfo.java
blob3cf5cac7ce3d123fe1489b7be6f50c9899f1a49d
1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // SquirrelJME
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the Mozilla Public License Version 2.0.
7 // See license.mkd for licensing and copyright information.
8 // ---------------------------------------------------------------------------
10 package cc.squirreljme.jvm.suite;
12 import cc.squirreljme.jvm.manifest.JavaManifestAttributes;
13 import cc.squirreljme.runtime.cldc.util.StringUtils;
14 import cc.squirreljme.runtime.cldc.util.UnmodifiableIterator;
15 import java.lang.ref.Reference;
16 import java.lang.ref.WeakReference;
17 import java.util.Arrays;
18 import java.util.Collection;
19 import java.util.Iterator;
20 import java.util.LinkedHashSet;
21 import java.util.Set;
23 /**
24 * This contains all of the information for dependencies which are provided
25 * by an application or library.
27 * @since 2017/11/30
29 public final class ProvidedInfo
30 implements Iterable<MarkedProvided>
32 /** Provided fields. */
33 private final MarkedProvided[] _provided;
35 /** String representation. */
36 private Reference<String> _string;
38 /**
39 * Initializes the provided dependency info.
41 * @param __provs The provided set.
42 * @since 2017/12/31
44 public ProvidedInfo(MarkedProvided... __provs)
46 // Defensive copy
47 __provs = (__provs == null ? new MarkedProvided[0] : __provs.clone());
49 // Check for nulls
50 for (MarkedProvided o : __provs)
51 if (o == null)
52 throw new NullPointerException("NARG");
54 this._provided = __provs;
57 /**
58 * Initializes the provided dependency info.
60 * @param __provs The provided set.
61 * @throws NullPointerException On null arguments.
62 * @since 2017/12/31
64 public ProvidedInfo(Collection<MarkedProvided> __provs)
65 throws NullPointerException
67 this(__provs.<MarkedProvided>toArray(
68 new MarkedProvided[__provs.size()]));
71 /**
72 * {@inheritDoc}
73 * @since 2017/12/31
75 @Override
76 public final boolean equals(Object __o)
78 if (this == __o)
79 return true;
81 if (!(__o instanceof ProvidedInfo))
82 return false;
84 return Arrays.equals(this._provided, ((ProvidedInfo)__o)._provided);
87 /**
88 * {@inheritDoc}
89 * @since 2017/12/31
91 @Override
92 public final int hashCode()
94 return Arrays.asList(this._provided).hashCode();
97 /**
98 * {@inheritDoc}
99 * @since 2021/01/31
101 @Override
102 public Iterator<MarkedProvided> iterator()
104 return UnmodifiableIterator.of(this._provided);
108 * {@inheritDoc}
109 * @since 2017/12/31
111 @Override
112 public final String toString()
114 Reference<String> ref = this._string;
115 String rv;
117 if (ref == null || null == (rv = ref.get()))
118 this._string = new WeakReference<>(
119 (rv = Arrays.asList(this._provided).toString()));
121 return rv;
125 * Parses the given suite information and returns all of the provided
126 * resolutions for dependencies which are specified in the manifest.
128 * @param __info The suite information to parse.
129 * @return The parsed provided resolution information.
130 * @throws InvalidSuiteException If the manifest is not correct.
131 * @throws NullPointerException On null arguments.
132 * @since 2017/11/20
134 public static ProvidedInfo of(SuiteInfo __info)
135 throws InvalidSuiteException, NullPointerException
137 if (__info == null)
138 throw new NullPointerException("NARG");
140 Set<MarkedProvided> provided = new LinkedHashSet<>();
141 JavaManifestAttributes attr = __info.manifest().getMainAttributes();
142 String value;
144 // Defined configurations
145 value = attr.getValue("X-SquirrelJME-DefinedConfigurations");
146 if (value != null)
147 for (String s : StringUtils.basicSplit(" \t", value))
148 provided.add(new Configuration(s));
150 // Defined profiles
151 value = attr.getValue("X-SquirrelJME-DefinedProfiles");
152 if (value != null)
153 for (String s : StringUtils.basicSplit(" \t", value))
154 provided.add(new Profile(s));
156 // Defined standards
157 value = attr.getValue("X-SquirrelJME-DefinedStandards");
158 if (value != null)
159 for (String s : StringUtils.basicSplit(",", value))
160 provided.add(new Standard(s.trim()));
162 // Has internal project name?
163 String internalname = attr.getValue(
164 "X-SquirrelJME-InternalProjectName");
165 if (internalname != null)
166 provided.add(new InternalName(internalname));
168 // This provides a library or otherwise
169 provided.add(new TypedSuite(__info.type(), __info.suite()));
171 // Initialize
172 return new ProvidedInfo(provided);