Search in sources :

Example 41 with TextValue

use of com.google.api.ads.admanager.jaxws.v202205.TextValue 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.v202108.DateTimeValue) NumberValue(com.google.api.ads.admanager.axis.v202108.NumberValue) TextValue(com.google.api.ads.admanager.axis.v202108.TextValue) DateValue(com.google.api.ads.admanager.axis.v202108.DateValue) BooleanValue(com.google.api.ads.admanager.axis.v202108.BooleanValue) 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) ParseException(java.text.ParseException) TargetingValue(com.google.api.ads.admanager.axis.v202108.TargetingValue) SetValue(com.google.api.ads.admanager.axis.v202108.SetValue)

Example 42 with TextValue

use of com.google.api.ads.admanager.jaxws.v202205.TextValue 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}, and {@link DateTimeValue} for type {@link DateTime}.
 * 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.getValues().addAll(values);
            return setValue;
        } else {
            throw new IllegalArgumentException("Unsupported Value type [" + value.getClass() + "]");
        }
    }
}
Also used : Set(java.util.Set) ResultSet(com.google.api.ads.admanager.jaxws.v202111.ResultSet) LinkedHashSet(java.util.LinkedHashSet) DateTimeValue(com.google.api.ads.admanager.jaxws.v202111.DateTimeValue) Targeting(com.google.api.ads.admanager.jaxws.v202111.Targeting) DateTime(com.google.api.ads.admanager.jaxws.v202111.DateTime) Date(com.google.api.ads.admanager.jaxws.v202111.Date) NumberValue(com.google.api.ads.admanager.jaxws.v202111.NumberValue) TextValue(com.google.api.ads.admanager.jaxws.v202111.TextValue) DateValue(com.google.api.ads.admanager.jaxws.v202111.DateValue) BooleanValue(com.google.api.ads.admanager.jaxws.v202111.BooleanValue) DateTimeValue(com.google.api.ads.admanager.jaxws.v202111.DateTimeValue) TextValue(com.google.api.ads.admanager.jaxws.v202111.TextValue) SetValue(com.google.api.ads.admanager.jaxws.v202111.SetValue) NumberValue(com.google.api.ads.admanager.jaxws.v202111.NumberValue) TargetingValue(com.google.api.ads.admanager.jaxws.v202111.TargetingValue) Value(com.google.api.ads.admanager.jaxws.v202111.Value) BooleanValue(com.google.api.ads.admanager.jaxws.v202111.BooleanValue) DateValue(com.google.api.ads.admanager.jaxws.v202111.DateValue) TargetingValue(com.google.api.ads.admanager.jaxws.v202111.TargetingValue) SetValue(com.google.api.ads.admanager.jaxws.v202111.SetValue)

Example 43 with TextValue

use of com.google.api.ads.admanager.jaxws.v202205.TextValue 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}, and {@link DateTimeValue} for type {@link DateTime}.
 * 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.getValues().addAll(values);
            return setValue;
        } else {
            throw new IllegalArgumentException("Unsupported Value type [" + value.getClass() + "]");
        }
    }
}
Also used : Set(java.util.Set) ResultSet(com.google.api.ads.admanager.jaxws.v202202.ResultSet) LinkedHashSet(java.util.LinkedHashSet) DateTimeValue(com.google.api.ads.admanager.jaxws.v202202.DateTimeValue) Targeting(com.google.api.ads.admanager.jaxws.v202202.Targeting) DateTime(com.google.api.ads.admanager.jaxws.v202202.DateTime) Date(com.google.api.ads.admanager.jaxws.v202202.Date) NumberValue(com.google.api.ads.admanager.jaxws.v202202.NumberValue) TextValue(com.google.api.ads.admanager.jaxws.v202202.TextValue) DateValue(com.google.api.ads.admanager.jaxws.v202202.DateValue) BooleanValue(com.google.api.ads.admanager.jaxws.v202202.BooleanValue) SetValue(com.google.api.ads.admanager.jaxws.v202202.SetValue) DateValue(com.google.api.ads.admanager.jaxws.v202202.DateValue) TextValue(com.google.api.ads.admanager.jaxws.v202202.TextValue) BooleanValue(com.google.api.ads.admanager.jaxws.v202202.BooleanValue) DateTimeValue(com.google.api.ads.admanager.jaxws.v202202.DateTimeValue) NumberValue(com.google.api.ads.admanager.jaxws.v202202.NumberValue) Value(com.google.api.ads.admanager.jaxws.v202202.Value) TargetingValue(com.google.api.ads.admanager.jaxws.v202202.TargetingValue) TargetingValue(com.google.api.ads.admanager.jaxws.v202202.TargetingValue) SetValue(com.google.api.ads.admanager.jaxws.v202202.SetValue)

Example 44 with TextValue

use of com.google.api.ads.admanager.jaxws.v202205.TextValue 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).isValue();
    } else if (value instanceof NumberValue) {
        if (((NumberValue) value).getValue() == null) {
            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) {
        List<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.jaxws.v202205.DateTimeValue) NumberValue(com.google.api.ads.admanager.jaxws.v202205.NumberValue) TextValue(com.google.api.ads.admanager.jaxws.v202205.TextValue) DateValue(com.google.api.ads.admanager.jaxws.v202205.DateValue) BooleanValue(com.google.api.ads.admanager.jaxws.v202205.BooleanValue) SetValue(com.google.api.ads.admanager.jaxws.v202205.SetValue) TextValue(com.google.api.ads.admanager.jaxws.v202205.TextValue) NumberValue(com.google.api.ads.admanager.jaxws.v202205.NumberValue) Value(com.google.api.ads.admanager.jaxws.v202205.Value) TargetingValue(com.google.api.ads.admanager.jaxws.v202205.TargetingValue) BooleanValue(com.google.api.ads.admanager.jaxws.v202205.BooleanValue) DateValue(com.google.api.ads.admanager.jaxws.v202205.DateValue) DateTimeValue(com.google.api.ads.admanager.jaxws.v202205.DateTimeValue) ParseException(java.text.ParseException) TargetingValue(com.google.api.ads.admanager.jaxws.v202205.TargetingValue) SetValue(com.google.api.ads.admanager.jaxws.v202205.SetValue)

Example 45 with TextValue

use of com.google.api.ads.admanager.jaxws.v202205.TextValue 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}, and {@link DateTimeValue} for type {@link DateTime}.
 * 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.getValues().addAll(values);
            return setValue;
        } else {
            throw new IllegalArgumentException("Unsupported Value type [" + value.getClass() + "]");
        }
    }
}
Also used : Set(java.util.Set) ResultSet(com.google.api.ads.admanager.jaxws.v202205.ResultSet) LinkedHashSet(java.util.LinkedHashSet) DateTimeValue(com.google.api.ads.admanager.jaxws.v202205.DateTimeValue) Targeting(com.google.api.ads.admanager.jaxws.v202205.Targeting) DateTime(com.google.api.ads.admanager.jaxws.v202205.DateTime) Date(com.google.api.ads.admanager.jaxws.v202205.Date) NumberValue(com.google.api.ads.admanager.jaxws.v202205.NumberValue) TextValue(com.google.api.ads.admanager.jaxws.v202205.TextValue) DateValue(com.google.api.ads.admanager.jaxws.v202205.DateValue) BooleanValue(com.google.api.ads.admanager.jaxws.v202205.BooleanValue) SetValue(com.google.api.ads.admanager.jaxws.v202205.SetValue) TextValue(com.google.api.ads.admanager.jaxws.v202205.TextValue) NumberValue(com.google.api.ads.admanager.jaxws.v202205.NumberValue) Value(com.google.api.ads.admanager.jaxws.v202205.Value) TargetingValue(com.google.api.ads.admanager.jaxws.v202205.TargetingValue) BooleanValue(com.google.api.ads.admanager.jaxws.v202205.BooleanValue) DateValue(com.google.api.ads.admanager.jaxws.v202205.DateValue) DateTimeValue(com.google.api.ads.admanager.jaxws.v202205.DateTimeValue) TargetingValue(com.google.api.ads.admanager.jaxws.v202205.TargetingValue) SetValue(com.google.api.ads.admanager.jaxws.v202205.SetValue)

Aggregations

LinkedHashSet (java.util.LinkedHashSet)40 Test (org.junit.Test)20 ParseException (java.text.ParseException)10 Set (java.util.Set)10 Before (org.junit.Before)10 BooleanValue (com.google.api.ads.admanager.axis.v202105.BooleanValue)5 DateTimeValue (com.google.api.ads.admanager.axis.v202105.DateTimeValue)5 DateValue (com.google.api.ads.admanager.axis.v202105.DateValue)5 NumberValue (com.google.api.ads.admanager.axis.v202105.NumberValue)5 SetValue (com.google.api.ads.admanager.axis.v202105.SetValue)5 TargetingValue (com.google.api.ads.admanager.axis.v202105.TargetingValue)5 TextValue (com.google.api.ads.admanager.axis.v202105.TextValue)5 BooleanValue (com.google.api.ads.admanager.axis.v202108.BooleanValue)5 DateTimeValue (com.google.api.ads.admanager.axis.v202108.DateTimeValue)5 DateValue (com.google.api.ads.admanager.axis.v202108.DateValue)5 NumberValue (com.google.api.ads.admanager.axis.v202108.NumberValue)5 SetValue (com.google.api.ads.admanager.axis.v202108.SetValue)5 TargetingValue (com.google.api.ads.admanager.axis.v202108.TargetingValue)5 TextValue (com.google.api.ads.admanager.axis.v202108.TextValue)5 BooleanValue (com.google.api.ads.admanager.jaxws.v202108.BooleanValue)5