class QueryOpinionService {

	static boolean secondaryAttributeMatches(String eFlagsDecimalString, String attribute) {

		// eFlagDecimalString is the elf e_flags value, as a decimal string
		// sa is the secondary attribute string from the opinion file,
		// and it must start with "0x" or "0b"
		if (attribute == null) {
			return false;
		}

		if (!StringUtils.startsWithAny(attribute.toLowerCase(), "0x", "0b")) {
			return false;
		}

		int eFlagsInt = Integer.parseInt(eFlagsDecimalString);
		String eFlagsBinaryString = Integer.toBinaryString(eFlagsInt);
		if (eFlagsBinaryString == null) {
			return false;
		}

		eFlagsBinaryString = StringUtils.leftPad(eFlagsBinaryString, 32, "0");
		String eFlagsHexString = Integer.toHexString(eFlagsInt);
		eFlagsHexString = StringUtils.leftPad(eFlagsHexString, 8, "0");

		// Remove '_' and whitespace from the attribute string
		String cleaned = attribute.replace("_", "").replaceAll("\\s+", "").trim();
		String prefix = cleaned.substring(0, 2);
		String value = cleaned.substring(2);
		if (prefix.toLowerCase().startsWith("0x")) {

			// It's a hex string
			value = StringUtils.leftPad(value, 8, "0");
			return value.equals(eFlagsHexString);
		}

		// It's a binary string
		value = StringUtils.leftPad(value, 32, "0");
		for (int i = 0; i < 32; i++) {
			char c = value.charAt(i);
			if (c == '.') { // wildcard
				continue;
			}

			if (eFlagsBinaryString.charAt(i) != c) {
				return false;
			}
		}

		return true;
	}

}