001/**
002 *
003 * Copyright 2014 Vyacheslav Blinov
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smackx.amp;
018
019import org.jivesoftware.smack.SmackException.NoResponseException;
020import org.jivesoftware.smack.SmackException.NotConnectedException;
021import org.jivesoftware.smack.XMPPConnection;
022import org.jivesoftware.smack.XMPPException.XMPPErrorException;
023import org.jivesoftware.smack.util.XmppDateTime;
024import org.jivesoftware.smackx.amp.packet.AMPExtension;
025
026import java.util.Date;
027
028
029public class AMPExpireAtCondition implements AMPExtension.Condition {
030
031    public static final String NAME = "expire-at";
032
033    /**
034     * Check if server supports expire-at condition
035     * @param connection Smack connection instance
036     * @return true if expire-at condition is supported.
037     * @throws XMPPErrorException 
038     * @throws NoResponseException 
039     * @throws NotConnectedException 
040     */
041    public static boolean isSupported(XMPPConnection connection) throws NoResponseException, XMPPErrorException, NotConnectedException  {
042        return AMPManager.isConditionSupported(connection, NAME);
043    }
044
045    private final String value;
046
047    /**
048     * Create new expire-at amp condition with value setted as XEP-0082 formatted date.
049     * @param utcDateTime Date instance of time
050     *                    that will be used as value parameter after formatting to XEP-0082 format. Can't be null.
051     */
052    public AMPExpireAtCondition(Date utcDateTime) {
053        if (utcDateTime == null)
054            throw new NullPointerException("Can't create AMPExpireAtCondition with null value");
055        this.value = XmppDateTime.formatXEP0082Date(utcDateTime);
056    }
057
058    /**
059     * Create new expire-at amp condition with defined value.
060     * @param utcDateTime UTC time string that will be used as value parameter.
061     *                    Should be formatted as XEP-0082 Date format. Can't be null.
062     */
063    public AMPExpireAtCondition(String utcDateTime) {
064        if (utcDateTime == null)
065            throw new NullPointerException("Can't create AMPExpireAtCondition with null value");
066        this.value = utcDateTime;
067    }
068
069    @Override
070    public String getName() {
071        return NAME;
072    }
073
074    @Override
075    public String getValue() {
076        return value;
077    }
078
079}