Search in sources :

Example 56 with SetValue

use of com.google.api.ads.admanager.jaxws.v202205.SetValue in project googleads-java-lib by googleads.

the class Pql method createValue.

/**
 * Creates a {@link Value} from the value i.e. a {@link TextValue} for a value of type {@code
 * String}, {@link BooleanValue} for type {@code Boolean}, {@link NumberValue} for type {@code
 * Double}, {@code Long}, or {@code Integer}, {@link DateTimeValue} for type {@link DateTime}, and
 * {@link DateValue} for type {@link Date}. If the value is a {@code Value}, the value is
 * returned. If the value is {@code null}, an empty {@link TextValue} is returned.
 *
 * @param value the value to convert
 * @return the constructed value of the appropriate type
 * @throws IllegalArgumentException if value cannot be converted
 */
public static Value createValue(Object value) {
    if (value instanceof Value) {
        return (Value) value;
    } else if (value == null) {
        return new TextValue();
    } else {
        if (value instanceof Boolean) {
            BooleanValue booleanValue = new BooleanValue();
            booleanValue.setValue((Boolean) value);
            return booleanValue;
        } else if (value instanceof Double || value instanceof Long || value instanceof Integer) {
            NumberValue numberValue = new NumberValue();
            numberValue.setValue(value.toString());
            return numberValue;
        } else if (value instanceof String) {
            TextValue textValue = new TextValue();
            textValue.setValue((String) value);
            return textValue;
        } else if (value instanceof DateTime) {
            DateTimeValue dateTimeValue = new DateTimeValue();
            dateTimeValue.setValue((DateTime) value);
            return dateTimeValue;
        } else if (value instanceof Date) {
            DateValue dateValue = new DateValue();
            dateValue.setValue((Date) value);
            return dateValue;
        } else if (value instanceof Targeting) {
            TargetingValue targetingValue = new TargetingValue();
            targetingValue.setValue((Targeting) value);
            return targetingValue;
        } else if (value instanceof Set<?>) {
            SetValue setValue = new SetValue();
            Set<Value> values = new LinkedHashSet<Value>();
            for (Object entry : (Set<?>) value) {
                validateSetValueEntryForSet(createValue(entry), values);
                values.add(createValue(entry));
            }
            setValue.setValues(values.toArray(new Value[] {}));
            return setValue;
        } else {
            throw new IllegalArgumentException("Unsupported Value type [" + value.getClass() + "]");
        }
    }
}
Also used : ResultSet(com.google.api.ads.admanager.axis.v202202.ResultSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) DateTimeValue(com.google.api.ads.admanager.axis.v202202.DateTimeValue) Targeting(com.google.api.ads.admanager.axis.v202202.Targeting) DateTime(com.google.api.ads.admanager.axis.v202202.DateTime) Date(com.google.api.ads.admanager.axis.v202202.Date) NumberValue(com.google.api.ads.admanager.axis.v202202.NumberValue) TextValue(com.google.api.ads.admanager.axis.v202202.TextValue) DateValue(com.google.api.ads.admanager.axis.v202202.DateValue) BooleanValue(com.google.api.ads.admanager.axis.v202202.BooleanValue) BooleanValue(com.google.api.ads.admanager.axis.v202202.BooleanValue) NumberValue(com.google.api.ads.admanager.axis.v202202.NumberValue) TargetingValue(com.google.api.ads.admanager.axis.v202202.TargetingValue) Value(com.google.api.ads.admanager.axis.v202202.Value) DateValue(com.google.api.ads.admanager.axis.v202202.DateValue) SetValue(com.google.api.ads.admanager.axis.v202202.SetValue) DateTimeValue(com.google.api.ads.admanager.axis.v202202.DateTimeValue) TextValue(com.google.api.ads.admanager.axis.v202202.TextValue) TargetingValue(com.google.api.ads.admanager.axis.v202202.TargetingValue) SetValue(com.google.api.ads.admanager.axis.v202202.SetValue)

Example 57 with SetValue

use of com.google.api.ads.admanager.jaxws.v202205.SetValue in project googleads-java-lib by googleads.

the class Pql method getApiValue.

/**
 * Gets the underlying value of the {@code Value} object that's comparable to what would be
 * returned in any other API object (i.e. DateTimeValue will return an API DateTime, not a Joda
 * DateTime).
 *
 * @param value the value to convert
 * @return the native value of {@code Value} or {@code null} if the underlying value is null
 * @throws IllegalArgumentException if value cannot be converted
 */
public static Object getApiValue(Value value) {
    if (value instanceof BooleanValue) {
        return ((BooleanValue) value).getValue();
    } else if (value instanceof NumberValue) {
        if (Strings.isNullOrEmpty(((NumberValue) value).getValue())) {
            return null;
        } else {
            try {
                return NumberFormat.getInstance().parse(((NumberValue) value).getValue());
            } catch (ParseException e) {
                throw new IllegalStateException("Received invalid number format from API: " + ((NumberValue) value).getValue());
            }
        }
    } else if (value instanceof TextValue) {
        return ((TextValue) value).getValue();
    } else if (value instanceof DateTimeValue) {
        return ((DateTimeValue) value).getValue();
    } else if (value instanceof DateValue) {
        return ((DateValue) value).getValue();
    } else if (value instanceof TargetingValue) {
        return ((TargetingValue) value).getValue();
    } else if (value instanceof SetValue) {
        Value[] setValues = ((SetValue) value).getValues();
        Set<Object> apiValue = new LinkedHashSet<Object>();
        if (setValues != null) {
            for (Value setValue : setValues) {
                validateSetValueEntryForSet(getApiValue(setValue), apiValue);
                apiValue.add(getApiValue(setValue));
            }
        }
        return apiValue;
    } else {
        throw new IllegalArgumentException("Unsupported Value type [" + value.getClass() + "]");
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) DateTimeValue(com.google.api.ads.admanager.axis.v202202.DateTimeValue) NumberValue(com.google.api.ads.admanager.axis.v202202.NumberValue) TextValue(com.google.api.ads.admanager.axis.v202202.TextValue) DateValue(com.google.api.ads.admanager.axis.v202202.DateValue) BooleanValue(com.google.api.ads.admanager.axis.v202202.BooleanValue) BooleanValue(com.google.api.ads.admanager.axis.v202202.BooleanValue) NumberValue(com.google.api.ads.admanager.axis.v202202.NumberValue) TargetingValue(com.google.api.ads.admanager.axis.v202202.TargetingValue) Value(com.google.api.ads.admanager.axis.v202202.Value) DateValue(com.google.api.ads.admanager.axis.v202202.DateValue) SetValue(com.google.api.ads.admanager.axis.v202202.SetValue) DateTimeValue(com.google.api.ads.admanager.axis.v202202.DateTimeValue) TextValue(com.google.api.ads.admanager.axis.v202202.TextValue) ParseException(java.text.ParseException) TargetingValue(com.google.api.ads.admanager.axis.v202202.TargetingValue) SetValue(com.google.api.ads.admanager.axis.v202202.SetValue)

Example 58 with SetValue

use of com.google.api.ads.admanager.jaxws.v202205.SetValue in project googleads-java-lib by googleads.

the class Pql method createValue.

/**
 * Creates a {@link Value} from the value i.e. a {@link TextValue} for a value of type {@code
 * String}, {@link BooleanValue} for type {@code Boolean}, {@link NumberValue} for type {@code
 * Double}, {@code Long}, or {@code Integer}, {@link DateTimeValue} for type {@link DateTime}, and
 * {@link DateValue} for type {@link Date}. If the value is a {@code Value}, the value is
 * returned. If the value is {@code null}, an empty {@link TextValue} is returned.
 *
 * @param value the value to convert
 * @return the constructed value of the appropriate type
 * @throws IllegalArgumentException if value cannot be converted
 */
public static Value createValue(Object value) {
    if (value instanceof Value) {
        return (Value) value;
    } else if (value == null) {
        return new TextValue();
    } else {
        if (value instanceof Boolean) {
            BooleanValue booleanValue = new BooleanValue();
            booleanValue.setValue((Boolean) value);
            return booleanValue;
        } else if (value instanceof Double || value instanceof Long || value instanceof Integer) {
            NumberValue numberValue = new NumberValue();
            numberValue.setValue(value.toString());
            return numberValue;
        } else if (value instanceof String) {
            TextValue textValue = new TextValue();
            textValue.setValue((String) value);
            return textValue;
        } else if (value instanceof DateTime) {
            DateTimeValue dateTimeValue = new DateTimeValue();
            dateTimeValue.setValue((DateTime) value);
            return dateTimeValue;
        } else if (value instanceof Date) {
            DateValue dateValue = new DateValue();
            dateValue.setValue((Date) value);
            return dateValue;
        } else if (value instanceof Targeting) {
            TargetingValue targetingValue = new TargetingValue();
            targetingValue.setValue((Targeting) value);
            return targetingValue;
        } else if (value instanceof Set<?>) {
            SetValue setValue = new SetValue();
            Set<Value> values = new LinkedHashSet<Value>();
            for (Object entry : (Set<?>) value) {
                validateSetValueEntryForSet(createValue(entry), values);
                values.add(createValue(entry));
            }
            setValue.setValues(values.toArray(new Value[] {}));
            return setValue;
        } else {
            throw new IllegalArgumentException("Unsupported Value type [" + value.getClass() + "]");
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ResultSet(com.google.api.ads.admanager.axis.v202205.ResultSet) Set(java.util.Set) DateTimeValue(com.google.api.ads.admanager.axis.v202205.DateTimeValue) Targeting(com.google.api.ads.admanager.axis.v202205.Targeting) DateTime(com.google.api.ads.admanager.axis.v202205.DateTime) Date(com.google.api.ads.admanager.axis.v202205.Date) NumberValue(com.google.api.ads.admanager.axis.v202205.NumberValue) TextValue(com.google.api.ads.admanager.axis.v202205.TextValue) DateValue(com.google.api.ads.admanager.axis.v202205.DateValue) BooleanValue(com.google.api.ads.admanager.axis.v202205.BooleanValue) BooleanValue(com.google.api.ads.admanager.axis.v202205.BooleanValue) NumberValue(com.google.api.ads.admanager.axis.v202205.NumberValue) DateTimeValue(com.google.api.ads.admanager.axis.v202205.DateTimeValue) TargetingValue(com.google.api.ads.admanager.axis.v202205.TargetingValue) DateValue(com.google.api.ads.admanager.axis.v202205.DateValue) SetValue(com.google.api.ads.admanager.axis.v202205.SetValue) TextValue(com.google.api.ads.admanager.axis.v202205.TextValue) Value(com.google.api.ads.admanager.axis.v202205.Value) TargetingValue(com.google.api.ads.admanager.axis.v202205.TargetingValue) SetValue(com.google.api.ads.admanager.axis.v202205.SetValue)

Example 59 with SetValue

use of com.google.api.ads.admanager.jaxws.v202205.SetValue in project googleads-java-lib by googleads.

the class Pql method getApiValue.

/**
 * Gets the underlying value of the {@code Value} object that's comparable to what would be
 * returned in any other API object (i.e. DateTimeValue will return an API DateTime, not a Joda
 * DateTime).
 *
 * @param value the value to convert
 * @return the native value of {@code Value} or {@code null} if the underlying value is null
 * @throws IllegalArgumentException if value cannot be converted
 */
public static Object getApiValue(Value value) {
    if (value instanceof BooleanValue) {
        return ((BooleanValue) value).getValue();
    } else if (value instanceof NumberValue) {
        if (Strings.isNullOrEmpty(((NumberValue) value).getValue())) {
            return null;
        } else {
            try {
                return NumberFormat.getInstance().parse(((NumberValue) value).getValue());
            } catch (ParseException e) {
                throw new IllegalStateException("Received invalid number format from API: " + ((NumberValue) value).getValue());
            }
        }
    } else if (value instanceof TextValue) {
        return ((TextValue) value).getValue();
    } else if (value instanceof DateTimeValue) {
        return ((DateTimeValue) value).getValue();
    } else if (value instanceof DateValue) {
        return ((DateValue) value).getValue();
    } else if (value instanceof TargetingValue) {
        return ((TargetingValue) value).getValue();
    } else if (value instanceof SetValue) {
        Value[] setValues = ((SetValue) value).getValues();
        Set<Object> apiValue = new LinkedHashSet<Object>();
        if (setValues != null) {
            for (Value setValue : setValues) {
                validateSetValueEntryForSet(getApiValue(setValue), apiValue);
                apiValue.add(getApiValue(setValue));
            }
        }
        return apiValue;
    } else {
        throw new IllegalArgumentException("Unsupported Value type [" + value.getClass() + "]");
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) DateTimeValue(com.google.api.ads.admanager.axis.v202205.DateTimeValue) NumberValue(com.google.api.ads.admanager.axis.v202205.NumberValue) TextValue(com.google.api.ads.admanager.axis.v202205.TextValue) DateValue(com.google.api.ads.admanager.axis.v202205.DateValue) BooleanValue(com.google.api.ads.admanager.axis.v202205.BooleanValue) BooleanValue(com.google.api.ads.admanager.axis.v202205.BooleanValue) NumberValue(com.google.api.ads.admanager.axis.v202205.NumberValue) DateTimeValue(com.google.api.ads.admanager.axis.v202205.DateTimeValue) TargetingValue(com.google.api.ads.admanager.axis.v202205.TargetingValue) DateValue(com.google.api.ads.admanager.axis.v202205.DateValue) SetValue(com.google.api.ads.admanager.axis.v202205.SetValue) TextValue(com.google.api.ads.admanager.axis.v202205.TextValue) Value(com.google.api.ads.admanager.axis.v202205.Value) ParseException(java.text.ParseException) TargetingValue(com.google.api.ads.admanager.axis.v202205.TargetingValue) SetValue(com.google.api.ads.admanager.axis.v202205.SetValue)

Example 60 with SetValue

use of com.google.api.ads.admanager.jaxws.v202205.SetValue in project googleads-java-lib by googleads.

the class PqlTest method testCreateValue_commaTextSet.

@Test
public void testCreateValue_commaTextSet() {
    Set<String> textSet = new LinkedHashSet<String>();
    textSet.add("value1");
    textSet.add("comma \",\" separated");
    Value value1 = ((SetValue) Pql.createValue(textSet)).getValues(0);
    Value value2 = ((SetValue) Pql.createValue(textSet)).getValues(1);
    assertEquals("value1", ((TextValue) value1).getValue());
    assertEquals("comma \",\" separated", ((TextValue) value2).getValue());
}
Also used : LinkedHashSet(java.util.LinkedHashSet) DateValue(com.google.api.ads.admanager.axis.v202108.DateValue) TargetingValue(com.google.api.ads.admanager.axis.v202108.TargetingValue) DateTimeValue(com.google.api.ads.admanager.axis.v202108.DateTimeValue) BooleanValue(com.google.api.ads.admanager.axis.v202108.BooleanValue) TextValue(com.google.api.ads.admanager.axis.v202108.TextValue) Value(com.google.api.ads.admanager.axis.v202108.Value) SetValue(com.google.api.ads.admanager.axis.v202108.SetValue) NumberValue(com.google.api.ads.admanager.axis.v202108.NumberValue) SetValue(com.google.api.ads.admanager.axis.v202108.SetValue) Test(org.junit.Test)

Aggregations

LinkedHashSet (java.util.LinkedHashSet)70 Test (org.junit.Test)50 Set (java.util.Set)10 BooleanValue (com.google.api.ads.admanager.axis.v202105.BooleanValue)8 DateTimeValue (com.google.api.ads.admanager.axis.v202105.DateTimeValue)8 DateValue (com.google.api.ads.admanager.axis.v202105.DateValue)8 NumberValue (com.google.api.ads.admanager.axis.v202105.NumberValue)8 SetValue (com.google.api.ads.admanager.axis.v202105.SetValue)8 TargetingValue (com.google.api.ads.admanager.axis.v202105.TargetingValue)8 TextValue (com.google.api.ads.admanager.axis.v202105.TextValue)8 BooleanValue (com.google.api.ads.admanager.axis.v202111.BooleanValue)8 DateTimeValue (com.google.api.ads.admanager.axis.v202111.DateTimeValue)8 DateValue (com.google.api.ads.admanager.axis.v202111.DateValue)8 BooleanValue (com.google.api.ads.admanager.axis.v202108.BooleanValue)6 DateTimeValue (com.google.api.ads.admanager.axis.v202108.DateTimeValue)6 DateValue (com.google.api.ads.admanager.axis.v202108.DateValue)6 NumberValue (com.google.api.ads.admanager.axis.v202108.NumberValue)6 SetValue (com.google.api.ads.admanager.axis.v202108.SetValue)6 TargetingValue (com.google.api.ads.admanager.axis.v202108.TargetingValue)6 TextValue (com.google.api.ads.admanager.axis.v202108.TextValue)6